parse a csv file that contains commans in the fields with awk

后端 未结 4 924
独厮守ぢ
独厮守ぢ 2021-02-05 10:47

i have to use awk to print out 4 different columns in a csv file. The problem is the strings are in a $x,xxx.xx format. When I run the regular awk command.

awk -         


        
4条回答
  •  长发绾君心
    2021-02-05 11:26

    In order to let awk handle quoted fields that contain the field separator, you can use a small script I wrote called csvquote. It temporarily replaces the offending commas with nonprinting characters, and then you restore them at the end of your pipeline. Like this:

    csvquote testfile.csv | awk -F, {print $1} | csvquote -u
    

    This would also work with any other UNIX text processing program like cut:

    csvquote testfile.csv | cut -d, -f1 | csvquote -u
    

    You can get the csvquote code here: https://github.com/dbro/csvquote

提交回复
热议问题