日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

awk 实例练习 (三)

發(fā)布時(shí)間:2023/12/15 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 awk 实例练习 (三) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
awk 使用printf? ? #printf使用類(lèi)似于C語(yǔ)言 #字符轉(zhuǎn)換 zhuyupeng@zhuyupeng-PC?~ $ echo "65" | awk '{printf "%c\n",$0}' A ? zhuyupeng@zhuyupeng-PC?~ $ echo "99" | awk '{printf "%f\n",$0}' 99.000000 ? #格式化輸出 #打印名字,左對(duì)齊,使用‘-’ 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傳入?yún)?shù) 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 文件系統(tǒng)??????????????? 1K-塊???? 已用??? 可用 已用% 掛載點(diǎn) 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腳本 下面的腳本是將該命令翻譯成為一個(gè)完整腳本的形式: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
??? }?
? #腳本運(yùn)行是通過(guò)secureCRT 登陸遠(yuǎn)程的服務(wù)器運(yùn)行的,控制臺(tái)略有不同 ? [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
? ? #一個(gè)文件中如果有相同的行連續(xù)出現(xiàn)就只打印一次 ? 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變量指定分隔符的時(shí)候,FS一定要放在BEGIN部分 ? #!/bin/awk -f
#to call :passwd.awk /etc/passwd
#print out the first and fifth fields
BEGIN{
??? FS=":"
}
{ print $1,"\t",$5}?#第一域是帳號(hào)名,第五域是賬號(hào)所有者

? [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腳本傳遞參數(shù) ? 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:(前面已經(jīng)給出) ? [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 數(shù)組,awk數(shù)組是類(lèi)似于一個(gè)鍵值對(duì),既可以使用數(shù)字做下標(biāo),也可以使用字符串做下標(biāo) ? 前面介紹過(guò)split函數(shù),并使用了一個(gè)例子: $awk 'BEGIN {print split("123#456#789",myarray,"#")}' 3 上面例子中,split返回?cái)?shù)組myarray下標(biāo)數(shù),實(shí)際上myarray數(shù)組為: myarray[1]="123" myarray[2]="456" myarray[3]="789" ? 數(shù)組使用前不必定義,也不必指定數(shù)組元素個(gè)數(shù)。經(jīng)常使用循環(huán)來(lái)方位數(shù)組,一般這樣使用循環(huán): for(element in array ) print array[element] ? #下面腳本先將"123#456#789" 使用split環(huán)峰,再循環(huán)打印個(gè)數(shù)組元素 #!/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]
?????? }
}

#要運(yùn)行腳本 需要使用/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.統(tǒng)計(jì)Yellow、Orange和Red級(jí)別的人各是多少 2.俱樂(lè)部中有多少成年(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

總結(jié)

以上是生活随笔為你收集整理的awk 实例练习 (三)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。