1

I have a CSV file, in.csv, which has an integer array column:

1,2,4,"{100,200,300}",,1
1,2,4,"{100,200,300,400,500}",,2
1,2,4,"{100,200,300,600.900,1200,1500}",1,3

I want to have an output file, out.csv, like:

1,2,4,,1
1,2,4,,2
1,2,4,1,3

I tried:

cut -d , -f4 -- complement in.csv > out.csv

But it did not work out. (I also have the fifth column which has value for third row but not for others).

1

Using csvkit:

$ csvcut -C 4 in.csv
1,2,4,,1
1,2,4,,2
1,2,4,1,3

The -C flag specifies what columns to remove.

0

This will do the work

awk -F'[,]' '{print $1","$2","$3","$(NF-1)","$NF}' in.csv
  • -F'[,]' this for sit the delimiter to ','
  • $NF give you the last item
0

With sed you can simply do:

sed 's/,"{.*}"//' in.csv > out.csv

The result:

cat out.csv 
1,2,4,,1
1,2,4,,2
1,2,4,1,3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.