bash method to remove last 4 columns from csv file

后端 未结 8 1277
后悔当初
后悔当初 2021-01-01 21:48

Is there a way to use bash to remove the last four columns for some input CSV file? The last four columns can have fields that vary in length from line to line so it is not

相关标签:
8条回答
  • 2021-01-01 22:42
    cat data.csv | rev | cut -d, -f-5 | rev
    

    rev reverses the lines, so it doesn't matter if all the rows have the same number of columns, it will always remove the last 4. This only works if the last 4 columns don't contain any commas themselves.

    0 讨论(0)
  • 2021-01-01 22:42

    This awk solution in a hacked way

    awk -F, 'OFS=","{for(i=NF; i>=NF-4; --i) {$i=""}}{gsub(",,,,,","",$0);print $0}' temp.txt
    
    0 讨论(0)
提交回复
热议问题