Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I parse CSV files on the Linux command line? [closed]

Tags:

linux

unix

csv

How can I parse CSV files on the Linux command line?

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: https://www.rfc-editor.org/rfc/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(!).

like image 800
Joel Avatar asked Jun 30 '09 11:06

Joel


People also ask

How do I read a CSV file from line by line in Linux?

Each line of the file is a data record. You can use while shell loop to read comma-separated cvs file. IFS variable will set cvs separated to , (comma). The read command will read each line and store data into each field.

How split CSV file Linux?

To split large CSV (Comma-Separated Values) file into smaller files in Linux/Ubuntu use the split command and required arguments. split -d -l 10000 source. csv tempfile.

How do I convert a CSV file to a text file in Linux?

So just in case you are using a text only browser, or need to cut and paste, Lukas's answer is to go into your command line, navigate to the right directory and type the command: “cut -d, -f3 report_source. csv | sed 's/”//g > report_links. txt” (without the quotes).


1 Answers

csvtool is really good. Available in Debian / Ubuntu (apt-get install csvtool). Example:

csvtool namedcol Account,Cost input.csv > output.csv 

See the CSVTool manual page for usage tips.

like image 57
Lari Hotari Avatar answered Sep 25 '22 13:09

Lari Hotari