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.
Nilesh Govindrajan
Site & Server Administrator
iTech7
Similar
- My Experience with Fedora Linux
- How to reposition the minimize, maximize and close buttons in Lucid Lynx (Ubuntu 10.04)
- C++:Reverse a number
- Creating 3D web interfaces with free tools
- 2 Methods To Access Blocked Websites In School Or In Your Workspace (Without Using Proxies).
- Better user interfaces for apps that work with queues of files
- C++:Simple program to calculate total and percentage
- Using If Condition in C++
- C++:Use the Modulus(%) Operator to get reminder
- Distinguish between FLOAT and INTEGER