Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove column from a csv file directly from mac terminal?

Tags:

macos

csv

How would one remove the 3rd column for example from a csv file directly from the command line of the Mac terminal. I understand

 cut -d',' -f3 data.csv

extracts the column info out directly into the terminal, but I want the 3rd column to be entirely removed from the dataset. How can I do this via the terminal?

like image 469
user2896468 Avatar asked Nov 28 '13 19:11

user2896468


2 Answers

Try

cut -d',' -f1-2,4- data.csv
like image 81
uselpa Avatar answered Dec 03 '22 16:12

uselpa


All the examples seem a bit tricky if trying to delete multiple fields or if you want to see only several columns. As such I simply show only the columns I want. So if I only want to get columns 1,2 and 5 I'd do this:

cut -d, -f 1,2,5 hugeData.csv

NB: -d sets whatever the separator is in the file. In the example above it is a comma ,

like image 44
John Crawford Avatar answered Dec 03 '22 15:12

John Crawford