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?
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?
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]
}
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