Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash method to remove last 4 columns from csv file

Tags:

bash

csv

sed

awk

cut

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 sufficient to just delete a certain number of characters from the end of each row.

like image 968
user788171 Avatar asked Jan 19 '13 20:01

user788171


2 Answers

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.

like image 64
peteches Avatar answered Oct 18 '22 04:10

peteches


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.

like image 17
Perleone Avatar answered Oct 18 '22 03:10

Perleone