Search code examples
awkdouble-quotes

Text in double quote with awk, and colum not in this delimiter (")


I've this log file :

11/01/2023 (17:01) [INFO]       => root : "get()" from wrapper.py (line:156) in get()
11/01/2023 (17:01) [INFO]       => root : "get() : 200 " from wrapper.py (line:166) in get()
11/01/2023 (17:01) [ERROR]      => root : "<!> initialisation error : Expecting value: line 1 column 1 (char 0)" from main.py (line:453) in <module>()

and, with awk, I want to get, for example $1 and $3 column, AND the text in double quote, like this :

11/01/2023 [INFO] "get()" 
11/01/2023 [INFO] "get() : 200 "
11/01/2023 [ERROR] "<!> initialisation error : Expecting value: line 1 column 1 (char 0)"

For the columns, ok :

awk '{print $1, $3}' mylog.log

Like this, but with the other(s) colum(s), like $1 and $3 :

$ awk -F\" '{print $2}' mylog.log
get()
get() : 200
<!> initialisation error : Expecting value: line 1

Have an idea please ? Thanks

F.


Solution

  • Using gnu-awk you can set FPAT to a double quoted string or any other non-quoted, non-whitespace string:

    awk -v FPAT='"[^"]*"|[^"[:blank:]]+' '{print $1, $3, $7}' file
    
    11/01/2023 [INFO] "get()"
    11/01/2023 [INFO] "get() : 200 "
    11/01/2023 [ERROR] "<!> initialisation error : Expecting value: line 1 column 1 (char 0)"
    

    Or use this awk solution on any version:

    awk 'match($0, /"[^"]*"/) {
       print $1, $3, substr($0, RSTART, RLENGTH)
    }' file