Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use awk to extract a quoted field? [duplicate]

I am using

awk '{ printf "%s", $3 }'

to extract some field from a space delimited line. Of course I get partial results when the field is quoted with free spaces inside. May any body suggest a solution please?

like image 970
mmonem Avatar asked Aug 11 '10 13:08

mmonem


2 Answers

show your input file and desired output next time. To get quoted fields,

$ cat file
field1 field2 "field 3" field4 "field5"

$ awk -F'"' '{for(i=2;i<=NF;i+=2) print $i}' file
field 3
field5
like image 122
ghostdog74 Avatar answered Nov 06 '22 16:11

ghostdog74


This is actually quite difficult. I came up with the following awk script that splits the line manually and stores all fields in an array.

{
    s = $0
    i = 0
    split("", a)
    while ((m = match(s, /"[^"]*"/)) > 0) {
        # Add all unquoted fields before this field
        n = split(substr(s, 1, m - 1), t)
        for (j = 1; j <= n; j++)
            a[++i] = t[j]
        # Add this quoted field
        a[++i] = substr(s, RSTART + 1, RLENGTH - 2)
        s = substr(s, RSTART + RLENGTH)
        if (i >= 3) # We can stop once we have field 3
            break
    }
    # Process the remaining unquoted fields after the last quoted field
    n = split(s, t)
    for (j = 1; j <= n; j++)
        a[++i] = t[j]
    print a[3]
}
like image 34
schot Avatar answered Nov 06 '22 15:11

schot