Extracting Data from a file using cut and grep

Printer-friendly version

Ever thought of having a database as a single TEXT file ? You could do that, but how do you extract data from it ?

This tutorial deals with extracting data from a SINGLE file very much similar to a RDBMS SELECT with WHERE query.

Let's create a file with the following lines -

A;A1;A2;A3;A4;A5
B;B1;B2;B3;B4;B5
C;C1;C2;C3;C4;C5
D;D1;D2;D3;D4;D5
E;E1;E2;E3;E4;E5
F;F1;F2;F3;F4;F5

As you see above, data has been separated by semicolons (Wink.

Suppose you want to extract the 4th field from the file for the record which starts with C.

To do the above, use this -

$ grep ^C file | cut -f4 -d\;

Please note that this will give you the value which says C3. This is because the first entry itself is a field.

In the above command, grep ^C means show all lines starting with the letter C.

And the next part cut -f4 -d\; means ONLY show the fourth field and since we have separated our fields by a semicolon, the -d\; option is used. Here you need to put the backslash before the semicolon because semicolon is a SHELL character. You could also use colon (Smile in the file and the cut command to avoid this problem.

Please comment if you have questions/suggestions.


Nilesh Govindrajan

Site & Server Administrator
iTech7

Syndicate content