I\'m looking for a simple way to print a specific field with awk while allowing for embedded spaces in the field.
Sample: Field1 Field2 \"Field Three\" Field
Mark Setchell's answer is good, although it will not work if you don't know in advance how many embedded quotes you have (and it doesn't split on spaces anymore).
I hacked this together (obviously it can be improved):
gawk -v FIELD=2 '{ a=$ FIELD; if (substr(a, 0, 1) == "\"") { gsub(/^\"/, "", a); s=a; for (i = FIELD + 1; i <= NF; i++) { a=$ i; nbSub=gsub(/\"$/, "", a); s = s " " a; if (nbSub > 0) { break } } print(s) } }' <<<'allo "hello world" bar'
I would recommend using something else than gawk for this (maybe look into parsing the fields with your shell's IFS variable?).
Addendum: As I said above, this is not really the right tool for the job. For example, you can specify the first field with the -v FIELD=, but it counts fields based on AWK's separator (the embedded spaces are still counted).