Parsing a CSV file using gawk

后端 未结 9 1257
感动是毒
感动是毒 2020-11-29 12:12

How do you parse a CSV file using gawk? Simply setting FS=\",\" is not enough, as a quoted field with a comma inside will be treated as multiple fields.

<
9条回答
  •  猫巷女王i
    2020-11-29 12:50

    You can use a simple wrapper function called csvquote to sanitize the input and restore it after awk is done processing it. Pipe your data through it at the start and end, and everything should work out ok:

    before:

    gawk -f mypgoram.awk input.csv
    

    after:

    csvquote input.csv | gawk -f mypgoram.awk | csvquote -u
    

    See https://github.com/dbro/csvquote for code and documentation.

提交回复
热议问题