Ignoring embeded spaces with AWK

后端 未结 4 1365
北海茫月
北海茫月 2020-12-20 09:00

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

4条回答
  •  生来不讨喜
    2020-12-20 09:26

    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).

提交回复
热议问题