awk 實例練習 (三)


awk 使用printf 
 
#printf使用類似於C語言
#字符轉換
zhuyupeng@zhuyupeng-PC ~
$ echo "65" | awk '{printf "%c\n",$0}'
A
 
zhuyupeng@zhuyupeng-PC ~
$ echo "99" | awk '{printf "%f\n",$0}'
99.000000
 
#格式化輸出
#打印名字,左對齊,使用‘-’
zhuyupeng@zhuyupeng-PC ~
$ awk '{printf "%-15s %s\n",$1,$3}' grade.txt
M.Tansley       48311
J.Lulu          48317
P.Bunny         48
J.Troll         4842
L.Tansley       4712
 
#向awk傳入參數
zhuyupeng@zhuyupeng-PC ~
$ awk '{if ($5 < AGE) print $0}' AGE=10 grade.txt
M.Tansley       05/99   48311   Green   8       40      44
J.Lulu  06/99   48317   green   9       24      26
 
zhuyupeng@zhuyupeng-PC ~
$ df -k
文件系統                1K-塊     已用    可用 已用% 掛載點
D:/Program Files/bin 76155900 70397660 5758240   93% /usr/bin
D:/Program Files/lib 76155900 70397660 5758240   93% /usr/lib
D:/Program Files     76155900 70397660 5758240   93% /
C:                   40857596 32552996 8304600   80% /cygdrive/c
D:                   76155900 70397660 5758240   93% /cygdrive/d
 
zhuyupeng@zhuyupeng-PC ~
$ df -k | awk '($4 ~/^[0-9]/) {if($4 > TRIGGER) print $6"\t"$4}' TRIGGER=80000
93%     70397660
93%     70397660
93%     70397660
/cygdrive/c     8304600
/cygdrive/d     5758240
  
 
#awk腳本
下面的腳本是將該命令翻譯成為一個完整腳本的形式: awk '(tot+=$6); END{print "Club student total points: " tot}' grade.txt
 
#!/bin/awk -f
#print a header first
BEGIN{
    print "Student  Date    Member No.   Grade   Age   Points   Max"
    print "Name     Joined                             Gained   Point Available"
    print "==================================================================="
}
#let's add the scores of points gained
(tot+=$6)
#finished processing
END{
    print "Club student total points :" tot
    print "Average Club Student points:" tot/NR
    } 
 
#腳本運行是通過secureCRT 登陸遠程的服務器運行的,控制台略有不同
 
[chen@localhost zyp]$ ./stu_tot.awk  grade.txt                                            
Student  Date    Member No.   Grade   Age   Points   Max
Name     Joined                             Gained   Point Available
===================================================================
M.Tansley       05/99   48311   Green   8       40      44
J.Lulu  06/99   48317   green   9       24      26
P.Bunny 02/99   48      Yellow  12      35      28
J.Troll 07/99   4842    Brown-3 12      26      26
L.Tansley       05/99   4712    Brown-2 12      30      28
Club student total points :155
Average Club Student points:31
 
 
#一個文件中如果有相同的行連續出現就只打印一次
 
strip.awk:
 
#!/bin/awk -f
#error_strip.awk
#to call: error_strip.awk <filename>
#strips out the ERROR* lines if there are more than one
#ERROR* lines after each filed record.
BEGIN{
    error_line=""
}
    #tell awk the whole is "ERROR *"
    {
        if ($0 == "ERROR*" && error_line == "ERROR*")
            next;
        error_line = $0;
        print
    }
 
stip.txt:
INVALID LCSD 98GJ23
ERROR*
ERROR*
CAUTION LPSS ERROR ON ACC NO.
ERROR*
ERROR*
ERROR*
ERROR*
ERROR*
PASS FILED INVALID ON GHSI
ERROR*
CUTION LPSS ERROR ON ACC NO.
ERROR*
ERROR*
 
[chen@localhost zyp]$ ./strip.awk strip.txt
INVALID LCSD 98GJ23
ERROR*
CAUTION LPSS ERROR ON ACC NO.
ERROR*
PASS FILED INVALID ON GHSI
ERROR*
CUTION LPSS ERROR ON ACC NO.
ERROR*
 
#在awk中使用FS變量指定分隔符的時候,FS一定要放在BEGIN部分
 
#!/bin/awk -f
#to call :passwd.awk /etc/passwd
#print out the first and fifth fields
BEGIN{
    FS=":"
}
{ print $1,"\t",$5} #第一域是帳號名,第五域是賬號所有者

 
[chen@localhost zyp]$ ./passwd.awk /etc/passwd
root     root
bin      bin
daemon   daemon
adm      adm
lp       lp
sync     sync
shutdown         shutdown
halt     halt
mail     mail
uucp     uucp
operator         operator
games    games
gopher   gopher
ftp      FTP User
nobody   Nobody
...
 
#向AWK腳本傳遞參數
 
age.awk:
 
#!/bin/awk -f
#name: age.awk
#to call : age.awk AGE=n grade.txt
#prints ages that are lower than the age supplied on the command line
{
    if ( $5 < AGE )
        print $0
}
 
grade.txt:(前面已經給出)
 
[chen@localhost zyp]$ ./age.awk AGE=10 grade.txt
M.Tansley       05/99   48311   Green   8       40      44
J.Lulu  06/99   48317   green   9       24      26
 
 
#awk 數組,awk數組是類似於一個鍵值對,既可以使用數字做下標,也可以使用字符串做下標
 
前面介紹過split函數,並使用了一個例子:
$awk 'BEGIN {print split("123#456#789",myarray,"#")}'
3
上面例子中,split返回數組myarray下標數,實際上myarray數組為:
myarray[1]="123"
myarray[2]="456"
myarray[3]="789"
 
數組使用前不必定義,也不必指定數組元素個數。經常使用循環來方位數組,一般這樣使用循環:
for(element in array ) print array[element]
 
#下面腳本先將 "123#456#789" 使用split環峰,再循環打印個數組元素
 
#!/bin/awk -f
#name: arraytest.awk
#prints out an array
BEGIN{
    record="123#456#789";
    split(record,myarray,"#")
}

END{
    for ( i in myarray )
       {
           print myarray[i]
       }
}

#要運行腳本 需要使用/dev/null作為輸入文件
[chen@localhost zyp]$ ./arraytest.awk  /dev/null
123
456
789
 
 
grade_student.txt:
 
Yellow#Junior
Orange#Senior
Yellow#Junior
Purple#Junior
Brown-2#Junior
White#Senior
Orange#Senior
Red#Junior
Brown-2#Senior
Yellow#Senior
Red#Junior
Blue#Senior
Green#Senior
Purple#Junior
White#Junior
 
 
belts.awk:
 
#!/bin/awk -f
#name: belts.awk
#to call: belts.awk grade2.txt
#loops through the grade2.txt file and counts how many
#belts we have in(yellow,orange,red)
#also count how many adults and juniors we have
#
#start of BEGIN
#set FS and load the arrays and our values
BEGIN{
    FS="#"
    #load the belt colours we are interested in only
    belt["Yellow"]
    belt["Orange"]
    belt["Red"]
    #end of BEGIN
    #load the student type
    student["Junior"]
    student["Senior"]
}
#loop thru array that holds the belt colours against field-1
#if we have a match,keep a running total
{ for (colour in belt)
    {
        if ($1==colour)
            belt[colour]++
    }
}
    #loop thru array that holds the student type against
    #field-2 if we have a match, keep a running total
       { for(senior_or_junior in student)
           {
               if($2 == senior_or_junior)
                   student[senior_or_junior]++
           }
       }
    #finished processing so print out the matches..for each array
END{ for(colour in belt)
       print "The club has",belt[colour],colour,"Belts"

        for(senior_or_junior in student)
             print "The club has",student[senior_or_junior]\
                            , senior_or_junior, "students"
   }


##
 
##
腳本的作用:
1.統計Yellow、Orange和Red級別的人各是多少
2.俱樂部中有多少成年(Senior)和未成年人(Junior)

 
 
 
#
[chen@localhost ~]$ ./belts.awk grade_student.txt
The club has 2 Red Belts
The club has 2 Orange Belts
The club has 3 Yellow Belts
The club has 7 Senior students
The club has 8 Junior students


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM