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

后端 未结 4 912
独厮守ぢ
独厮守ぢ 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:24

    Oddly enough I had to tackle this problem some time ago and I kept the code around to do it. You almost had it, but you need to get a bit tricky with your field separator(s).

    awk -F'","|^"|"$' '{print $2}' testfile.csv 
    

    Input

    # cat testfile.csv
    "$141,818.88","$52,831,578.53","$52,788,069.53"
    "$2,558.20","$482,619.11","$9,687,142.69"
    "$786.48","$8,568,159.41","$159,180,818.00"
    

    Output

    # awk -F'","|^"|"$' '{print $2}' testfile.csv
    $141,818.88
    $2,558.20
    $786.48
    

    You'll note that the "first" field is actually $2 because of the field separator ^". Small price to pay for a short 1-liner if you ask me.

提交回复
热议问题