Extract specific columns from delimited file using Awk

后端 未结 8 1906
-上瘾入骨i
-上瘾入骨i 2020-11-28 05:44

Sorry if this is too basic. I have a csv file where the columns have a header row (v1, v2, etc.). I understand that to extract columns 1 and 2, I have to do: awk -F \

8条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-28 06:13

    As mentioned by @Tom, the cut and awk approaches actually don't work for CSVs with quoted strings. An alternative is a module for python that provides the command line tool csvfilter. It works like cut, but properly handles CSV column quoting:

    csvfilter -f 1,3,5 in.csv > out.csv
    

    If you have python (and you should), you can install it simply like this:

    pip install csvfilter
    

    Please take note that the column indexing in csvfilter starts with 0 (unlike awk, which starts with $1). More info at https://github.com/codeinthehole/csvfilter/

提交回复
热议问题