Linux Shell Scripting Tutorial (LSST) v1.05r3 | ||
Chapter 7: awk Revisited | ||
|
Consider following text database file
Sr.No | Product | Qty | Unit Price |
1 | Pen | 5 | 20.00 |
2 | Rubber | 10 | 2.00 |
3 | Pencil | 3 | 3.50 |
4 | Cock | 2 | 45.50 |
In above file fields are Sr.No,Product,Qty,Unit Price. Field is the smallest element of any record. Each fields has its own attributes. For e.g. Take Qty. field. Qty. fields attribute is its numerical (Can contain only numerical data). Collection of fields is know as record. So
1. Pen 5 20.00 ----> Is a Record.
Collection of record is know as database file. In above text database file each field is separated using space (or tab character) and record is separated using new-line character ( i.e. each record is finished at the end of line ). In the awk, fields are access using special variable. For e.g. In above database $1, $2, $3, $4 respectively represents Sr.No, Product, Qty, Unit Price fields. (Don't confuse $1,$2 etc with command line arguments of shell script)
For this part of tutorial create text datafile inven (Shown as above). Now enter following simple awk program/command at shell prompt:
$ awk '{ print $1 $2 "--> Rs." $3 * $4 }' inven
1.Pen--> Rs.100
2.Pencil--> Rs.20
3.Rubber--> Rs.10.5
4.Cock--> Rs.91
Above awk program/command can be explained as follows:
awk program statement | Explanation |
'{ print $1 $2 "--> Rs." $3 * $4 } ' | print command is used to print contains of variables or text enclose in " text ". Here $1, $2, $3,$4 are all the special variable. $1, $2, etc all of the variable contains value of field. Finally we can directly do the calculation using $3 * $4 i.e. multiplication of third and fourth field in database. Note that "--> Rs." is string which is printed as its. |
Note $1,$2 etc (in awk) also know as predefined variable and can assign any value found in field.
Type following awk program at shell prompt,
$ awk '{ print $2 }' inven
Pen
Pencil
Rubber
Cock
awk prints second field from file. Same way if you want to print second and fourth field from file then give following command:
$awk '{ print $2 $4}' inven
Pen20.00
Pencil2.00
Rubber3.50
Cock45.50
$0 is special variable of awk , which print entire record, you can verify this by issuing following awk command:
$ awk '{ print $0 }' inven
1. Pen 5 20.00
2. Pencil 10 2.00
3. Rubber 3 3.50
4. Cock 2 45.50
You can also create awk command (program) file as follows:
$ cat > prn_pen |
And then you can execute or run above "prn_pen" awk command file as follows
$ awk -f prn_pen inven
5
10
In above awk program /Pen/ is the search pattern, if this pattern is found on line (or record) then print the third field of record.
{ print $3 } is called Action. On shell prompt , $ awk -f prn_pen inven , -f option instruct awk, to read its command from given file, inven is the name of database file which is taken as input for awk.
Now create following awk program as follows:
$cat > comp_inv |
Run it as follows:
$ awk -f comp_inv inven
2. Pencil 10 2.00
Here third field of database is compared with 5, this the pattern. If this pattern found on any line database, then entire record is printed.
awk Revisited | Predefined variable of awk |