Linux Shell Scripting Tutorial (LSST) v1.05r3 | ||
Chapter 5: Essential Utilities for Power User | ||
|
egg order 4
cacke good 10
cheese okay 4
pen good 12
floppy good 5
After crating file issue command
$ awk '/good/ { print $3 }' inventory
10
12
5
awk utility, select each record from file containing the word "good" and performs the action of printing the third field (Quantity of available goods.). Now try the following and note down its output.
$ awk '/good/ { print $1 " " $3 }' inventory
General Syntax of awk utility:
Syntax:
awk 'pattern action' {file-name}
For $ awk '/good/ { print $3 }' inventory example,
/good/ | Is the pattern used for selecting lines from file. |
{print $3} | This is the action; if pattern found, print on of such action. Here $3 means third record in selected record. (What $1 and $2 mean?) |
inventory | File which is used by awk utility which is use as input for awk utility. |
Use of awk utility:
To manipulate data.
Translating range of characters using tr utility | sed utility - Editing file without using editor |