bash method to remove last 4 columns from csv file

后端 未结 8 1319
后悔当初
后悔当初 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:40

    Cut can do this if all lines have the same number of fields or awk if you don't.

    cut -d, -f1-6 # assuming 10 fields
    

    Will print out the first 6 fields if you want to control the output seperater use --output-delimiter=string

    awk -F , -v OFS=, '{ for (i=1;i<=NF-4;i++){ printf $i, }; printf "\n"}'
    

    Loops over fields up to th number of fields -4 and prints them out.

提交回复
热议问题