The following will output "b1
as it recognizes the quoted space as a field delimiter. How do I tell awk to ignore quoted delimiters so that this would output b1 b2
or "b1 b2"
echo 'a "b1 b2" c'| awk '{print $2}'
I see the following two related posts, but I'm having trouble getting the solutions to work. I was hoping to find a simple solution. Field parsing is awk's specialty, right?
awk ignore delimiter inside single quote within a parenthesis What's the most robust way to efficiently parse CSV using awk?
With
gawk
(GNU awk) you can use theFPAT
special variable to define how a field looks like instead of being limited to specify a delimiter:Here we say: A field is either a
"
followed by non"
chars and a closing"
->("[^"]+")
... or|
a sequence of non-blank chars ->[^[:blank:]]+
These regexes will be evaluated in order, therefore a field enclosed in
""
has precedence over the second pattern, the sequence of non blank chars (awk's default).See GNU awk manual: Defining fields by content