//View Tip #569
Similar Tips
» List non-system users
» iso2cd
» Splitting arguments with read
» SSH Auto Complete
» Check memory and swap from command line


Latest tips by RSS
Click here to subscribe
Follow Shell-Fu on Twitter
Click here to follow
Follow Shell-Fu on identi.ca
Click here to follow
cut -d"," -f1,5 file.csv > newfile.csv

Will return only the 1st and 5th columns of a comma-delimited csv file.

View Comments »


Add your comment

Comments are currently disabled
Thank you very much for sharing this code with the community.  I learn a lot from shell-fu.  But you might want to document that this example will only work if your comma-separated values never contain commas.  Fields containing commas are quoted:

field_1, "hello, field two", field_3

Also, double-quotes in a CSV file are escaped with... double-quotes:

field_1, "I said, ""hello, field two""", field_3

I don't have a workaround, but documenting the limitation might be enough.  Maybe you or someone else will be inspired to provide an improved version.
Posted 2009-03-16 19:36:46
I changed code to
$ cut -d" " -f18,19 file.csv > newfile.csv
This saves column 18&19 in a SPACE (" ") delimited file to newfile.
Thanks for the original!
Posted 2009-06-15 15:16:25

Home Latest Browse Top 25 Random Hall Of Fame Contact Submit