Thursday, March 4, 2010

Programmer - Linux Tool To Parse CSV files

Programmer Question

Is anyone aware of a bash command that can be used to parse CSV files?



To do things like:



csvparse -c 2,5,6 filename



to extract fields from columns 2 5 and 6 from all rows.



It should be able to handle the csv file format: http://tools.ietf.org/html/rfc4180 which means quoting fields and escaping inner quotes as appropriate, so for an example row with 3 fields:




field1,"field, number ""2"", has inner
quotes and a comma",field3




so that if I request field 2 for the row above I get:




field, number "2", has inner quotes
and a comma




I appreciate that there are numerous solutions, perl, awk (etc..) to this problem but I would like a native bash command line tool that does not require me to invoke some other scripting environment or write any additional code (!).



Find the answer here

No comments:

Post a Comment

LinkWithin

Related Posts with Thumbnails