Wednesday, January 18, 2012

Command AWK


Shell script
As a novice in the world's script' shell, the command 'awk' is something that must I understand to be the level further. Why? because 'awk' proved to simplify my current job 
Well, Let's go .. 
What is the real 'awk' it? 
'AWK' asalah utility similar to 'sed' which serves to find a pattern in a file and perform an operation. 'AWK' own stands behind the names of its creators, namely Alfred V. Aho, Peter J. Weinberger, and Brian W. Kernighan. Hmm ... that's it seemed as the introduction of 'awk', we just go into how to use it. 
Now we try the basics first ... 
bash-3.00$ date
Mon May 17 10:29:45 WIB 2010
bash-3.00$ date | awk '{print $1}'
Mon
bash-3.00$ date | awk '{print $4}'
10:30:01
bash-3.00$ date | awk '{print $3,$2}'
17 May
bash-3.00$ date | awk '{print $3$2}'
17May

'Awk' can also be used in arithmetic operations, like addition (+), subtraction (-), multiplication (*), division (/), the rest of the division / modulus (%), and so on. For its use, can be seen in the following example: 
bash-3.00$ echo 2 3 | awk '{print $1 + $2}'
5
bash-3.00$ echo 2 3 | awk '{print $1 - $2}'
-1
bash-3.00$ echo 2 3 | awk '{print $1 * $2}'
6
bash-3.00$ echo 2 3 | awk '{print $1 / $2}'
0.666667
bash-3.00$ echo 5 3 | awk '{print $1 % $2}'
2

This time we will go next step make the script into a file and run it. 
first create a file with a name pelanggan.awk
bash-3.00$ vi pelanggan.awk

EXCELCOMINDO  INCOMING   42542    32230    254786
HUTCHINSON    INCOMING   2456     16953    573586
MOBILE-8      INCOMING   15944    11477    692939
MOBILSEL      INCOMING   1680     91188    951394
NATRINDO      INCOMING   2198     19826    154859
After that we save 
Now make the script. Create a file named print.awk
bash-3.00$ vi print.awk

BEGIN {
  print "\tKolom 1\tKolom 2"
  kolom1=0
  kolom2=0
}
{
  kolom1+=$3
  kolom2+=$4
  print "\t"$3"\t"$4
}
{
  kolom3=kolom1/5
  kolom4=kolom2/5
}
END {
    print "Jumlah:"kolom1"\t"kolom2
    print "Rata:\t"kolom3"\t"kolom4
}
Then save. To menjalanakannya type:
bash-3.00$ awk -f print.awk pelanggan.awk
The result:
        Kolom 1    Kolom 2
        42542      32230
        2456       16953
        15944      11477
        1680       91188
        2198       19826

Jumlah: 64820      171674
Rata:   12964      34334.8

All functions that are owned by the 'awk' can also be used together. For example, I want to add up the size of some files starting character file name is abe, then simply by typing: bash-3.00$ ls -l abe* | awk '{sum=sum + $5} END {print sum}'
197904

At the command ls-l shows the field size of a file is on the field to-five ($ 5), ls-l abe * be used to list files beginning with abe. As for the command 'awk' is used a variable sum to save the sum of $ 5 on each line / file name, the last line in the file list is displayed the value of the variable sum. 


Actually there are very many secrets of 'awk'. Finally, Thanks for your attention and good bye



Don't miss it
vote web ini yaa klik disini!!

No comments:

Post a Comment