tags:

views:

227

answers:

2

I have a set of csv files (around 250), each having 300 to 500 records. I need to cut 2 or 3 columns from each file and store it to another one. I'm using ubuntu OS. Is there any way to do it in command or utility?

+3  A: 

If you know that the column delimiter does not occur inside the fields, you can use cut.

$ cat in.csv
foo,bar,baz
qux,quux,quuux
$ cut -d, -f2,3 < in.csv 
bar,baz
quux,quuux

You can use the shell buildin 'for' to loop over all input files.

You can scalably process all files with something like:find "*.csv" | xargs cut -d, -f2,3 > out.csv
pixelbeat
You can use the `--output-delimiter=STRING` option of `cut` if you want to get rid of the comma or change it to something else.
Dennis Williamson
A: 

If the fields might contain the delimiter, you ought to find a library that can parse CSV files. Typically, general purpose scripting languages will include a CSV module in their standard library.

Ruby:   require 'csv'
Python: import csv
Perl:   use Text::ParseWords;
glenn jackman