Extracting Data from a file using cut and grep

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 (;).

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 (:) in the file and the cut command to avoid this problem.

Please comment if you have questions/suggestions.

Trackback URL for this post:

http://www.itech7.com/trackback/74

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <h1><h2><h3><h4><h5><h6><b><i><u><style><div><span><li><ol><ul><img><s><em><strong><a><address><blockquote><table><td><tr><th><caption><p><br><pre><code><fn><footnotes><dl><dt><dd><font>
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Use [fn]...[/fn] (or <fn>...</fn>) to insert automatically numbered footnotes.

More information about formatting options

CAPTCHA
This is to verify that you are human visitor
3 + 2 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.

Syndicate content
All trademarks and copyrights on this page are owned by their respective owners. Comments are owned by the Poster. Rest Copyright © iTech7.com

For License and Copyrights see http://www.itech7.com/Content-Copyrights