We have seen many times 'awk' command is used frequently in Linux shell scripts. But, nobody knows it's meaning and never try to know it, I am also included in this list:). That's why it's my small attempt to focus on 'awk' command ....
The awk command combines the functions of grep and sed, making it one of the most powerful Linux command
. Using 'awk' command, you can substitute words from an input file's lines for words in a template or perform calculations on numbers within a file.
It is mostly used with 'print' command...
. Using 'awk' command, you can substitute words from an input file's lines for words in a template or perform calculations on numbers within a file.
It is mostly used with 'print' command...
The general form of the awk command is
awk <pattern> '{print <stuff>}' <file>
Example.
Let's assume that file test.txt contains following word:
stone hammer wood
ball bat car
ball bat car
Now we'll use the print function in awk to plug the words from each input line into a template, like this:
awk '{print "Hit the",$1,"with your",$2}' test.txt
Hit the stone with your hammer
Hit the pedal with your foot
Hit the pedal with your foot
Test script that uses 'awk' command
#!/bin/bash
load=`cat /proc/loadavg|awk {' print $1 '}`
date=`date`
disp=$date.$load
echo $disp|tee -a loadavg.tx
load=`cat /proc/loadavg|awk {' print $1 '}`
date=`date`
disp=$date.$load
echo $disp|tee -a loadavg.tx
[root@server ~]# cat /proc/loadavg
0.15 0.56 0.31 1/84 2696
[root@server ~]#
2. By using awk {' print $1 '}` , we will cut first column ($1)
[root@server ~]# cat /proc/loadavg|awk {' print $1 '}
0.15
[root@server ~]#
Hope you would understand it..

No comments:
Post a Comment