Go2Linux | Linux Operating System

A site dedicated to: Linux Operating System

How to work with csv files in Linux

Date: 2010-11-02 00:00:00 -0400

These days I have been working with csv files (Comma, separated files).

Usually you deal with csv files using Open Office or Microsoft Excel, but if all you want to do is to extract some data from the csv (I mean columns) you can easily use cut.

Let’s see a csv file example.

United States,Washington,North America
Japan,Tokyo,Asia
Israel,Tel Aviv,Asia
Bolivia,Sucre,South America
Brasil,Brasilia,South America

Let’s say we only want the country and continent

cut -d',' -f1,3 file.csv

The output will be:

United States,North America
Japan,Asia
Israel,Asia
Bolivia,South America
Brasil,South America

The options used are:

-d, —delimiter

use DELIM instead of TAB for field delimiter

-f, —fields

select only these fields; also print any line that contains no delimiter character, unless the -s option is specified.

-s, —only-delimited

do not print lines not containing delimiters

Check the cut man page for more info.

If you liked this article please share it.

powered by TinyLetter

If you want to contact me in any other way, please use the contact page.