1.單條語句
語法:
if(conditional-expression ) action
if 是關鍵字
conditional-expression 是要檢測的條件表達式
action 是要執行的語句
2.多條語句
如果要執行多條語句,需要把他們放在{ } 中,每個語句之間必須用分號或換行符分開,如下所示.
語法:
if (conditional-expression) { action1; action2; }
如果條件為真,{ } 中的語句會依次執行。當所有語句執行完後,awk 會繼續執行後面的語句。
注意:與具體的花括號後面不能加分號。
實例1:打印數量小於等於 5 的所有商品
[root@localhost ~]# !cat cat items.txt 101,HD Camcorder,Video,210,10 102,Refrigerator,Appliance,850,2 103,MP3 Player,Audio,270,15 104,Tennis Racket,Sports,190,20 105,Laser Printer,Office,475,5 [root@localhost ~]# awk -F, '{ if($5<=5) print "Only",$5,"qty of",$2 "is available"}' items.txt Only 2 qty of Refrigeratoris available Only 5 qty of Laser Printeris available
實例2:打印價錢在 500 至 100,並且總數不超過 5 的商品
[root@localhost ~]# awk -F, '{ if (($4 >= 500 && $4<= 1000) && ($5 <= 5)) print "Only",$5,"qty of",$2,"is available" }' items.txt Only 2 qty of Refrigerator is available
在 if else 結構中, 還可以指定判斷條件為 false 時要執行的語句。 下面的語法中,如果條件 為 true,那麼執行 action1,如果條件為 false,則執行 action2
語法:
if (conditional-expression) action1 else action2
此外, awk 還有個條件操作符( ? : ), 和 C 語言的三元操作符等價。
和 if-else 結構相同,如果 codintional-expresion 是 true,執行 action1,否則執行 action2。
三元操作符:
codintional-expression ? action1 : action2 ;
實例1:如果商品數量不大於 5,打印”Buy More”,否則打印商品數量
[root@localhost ~]# cat if.awk BEGIN { FS=","; } { if( $5 <= 5) print "Buy More: Order",$2,"immediately!" else print "Shell More: Give discount on",$2,"immediately!" } [root@localhost ~]# cat items.txt 101,HD Camcorder,Video,210,10 102,Refrigerator,Appliance,850,2 103,MP3 Player,Audio,270,15 104,Tennis Racket,Sports,190,20 105,Laser Printer,Office,475,5 [root@localhost ~]# awk -f if.awk items.txt Shell More: Give discount on HD Camcorder immediately! Buy More: Order Refrigerator immediately! Shell More: Give discount on MP3 Player immediately! Shell More: Give discount on Tennis Racket immediately! Buy More: Order Laser Printer immediately!
實例2:用三元操作符,把 items.txt 文件中的每兩行都以逗號分隔合並起來
[root@localhost ~]# cat items.txt 101,HD Camcorder,Video,210,10 102,Refrigerator,Appliance,850,2 103,MP3 Player,Audio,270,15 104,Tennis Racket,Sports,190,20 105,Laser Printer,Office,475,5 [root@localhost ~]# awk 'ORS=NR%2?",":"\n"' items.txt 101,HD Camcorder,Video,210,10,102,Refrigerator,Appliance,850,2 103,MP3 Player,Audio,270,15,104,Tennis Racket,Sports,190,20 105,Laser Printer,Office,475,5,[root@localhost ~]#
相同於:
[root@localhost ~]# awk '{ if (NR %2 ==0) ORS = "\n";else ORS = ",";print}' items.txt 101,HD Camcorder,Video,210,10,102,Refrigerator,Appliance,850,2 103,MP3 Player,Audio,270,15,104,Tennis Racket,Sports,190,20 105,Laser Printer,Office,475,5,[root@localhost ~]#
語法:
while (codition) Actions
while 是 awk 的關鍵字
condition 是條件表達式
actions 是循環體,如果有多條語句,必須放在{ }中
while首先檢查 condtion,如果是 true,執行 actions,執行完後,再次檢查 condition,如果是 true, 再次執行 actions,直到 condition 為 false 時,退出循環。
實例1:
[root@localhost ~]# awk 'BEGIN { while(count++<50) string=string "x"; print string}' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
實例2:算 items-sold.txt 文件中,每件商品出售的總數
[root@localhost ~]# cat items-sold.txt 101 2 10 5 8 10 12 102 0 1 4 3 0 2 103 10 6 11 20 5 13 104 2 3 4 0 6 5 105 10 2 5 7 12 6 [root@localhost ~]# awk ' >{i=2;total=0;while(i<=NF) >{total=total+$i;i++;} >print total}' items-sold.txt 47 10 65 20 42
While 循環是一種進入控制判斷的循環結構,因為在進入循環體前執行判斷。而 do-while 循 環是一種退出控制循環,在退出循環前執行判斷。 do-while 循環至少會執行一次,如果條 件為 true,它將一直執行下去。
語法:
do action while(condition)
實例1:
[root@localhost ~]# awk 'BEGIN{ do print "this is a test";while (count++ < 5);}' this is a test this is a test this is a test this is a test this is a test this is a test
注意:首先執行一次,所以上述實例打印6次。
實例2:
[root@localhost ~]# cat dowhile.awk { i=2; total=0; do { total = total + $i; i++; } while(i<=NF) print "Item",$1,":",total,"quantities sold"; } [root@localhost ~]# awk -f dowhile.awk items-sold.txt Item 101 : 47 quantities sold Item 102 : 10 quantities sold Item 103 : 65 quantities sold Item 104 : 20 quantities sold Item 105 : 42 quantities sold
語法:
for(initialization;condition;increment/decrement)
for 循環一開始就執行 initialization,然後檢查 condition,如果 condition 為 true,執行 actions,然後執行 increment 或 decrement.如果 condition 為 true,就會一直重復執行 actions 和increment/decrement。
實例1:計算1到4的整數和。
[root@localhost ~]# echo "1 2 3 4"|awk '{for(i=1;i<=NF;i++) total+=$i;}END{print total}' 10
實例2:把文件中的字段反序打印出來
[root@localhost ~]# awk ' >BEGIN{ORS=""} >{for(i=NF;i>0;i--) >print $i," "; >print "\n"}' items-sold.txt 12 10 8 5 10 2 101 2 0 3 4 1 0 102 13 5 20 11 6 10 103 5 6 0 4 3 2 104 6 12 7 5 2 10 105
注意:在 awk 裡, print 和 printf 的區別是, print 輸出當前記錄(行 record), 並在最後自動加上 ORS ( 默認是\n ,但可以是任何字符串). 而 printf 是按給定格式輸出內容, 你給它 ORS 參數, 它就輸出 ORS, 沒給就不輸出.
Break 語句用來跳出它所在的最內層的循環(while,do-while,或 for 循環)。請注意, break 語句 只有在循環中才能使用。
實例1:打印某個月銷售量為 0 的任何商品
[root@localhost ~]# awk ' >{for(i=2;i<=NF;i++) >{if($i == 0) >{print $0;break;}}}' items-sold.txt 102 0 1 4 3 0 2 104 2 3 4 0 6 5
實例2:
[root@localhost ~]# awk 'BEGIN{while(1) {i++;if(i==10) break;print "young";}}' young young young young young young young young young
Continue 語句跳過後面剩余的循環部分,立即進入下次循環。 請注意, continue 只能用在循 環當中。
實例1:打印 items-sold.txt 文件中所有商品的總銷售量
[root@localhost ~]# awk ' >{total=0;for(i=1;i<=NF;i++) >{if(i == 1) continue;total+=$i}; >print total}' items-sold.txt 47 10 65 20 42
實例2:
[root@localhost ~]# awk 'BEGIN{for(i=1;i<=5;i++) >{if(i ==3 ) continue; >print "this is:",i,"x";}}' this is: 1 x this is: 2 x this is: 4 x this is: 5 x
exit 命令立即停止腳本的運行,並忽略腳本中其余的命令。 exit 命令接受一個數字參數最為 awk 的退出狀態碼,如果不提供參數,默認的狀態碼是 0.
實例1:
[root@localhost ~]# cat items-sold.txt 101 2 10 5 8 10 12 102 0 1 4 3 0 2 103 10 6 11 20 5 13 104 2 3 4 0 6 5 105 10 2 5 7 12 6 [root@localhost ~]# cat so { i=2;total=0; while(i++<=NF) if($i==0) { #某月份銷量為0則退出腳本 print "Item",$1,"had a month with no item sold"; exit; } } [root@localhost ~]# awk -f so items-sold.txt Item 102 had a month with no item sold
next提前結束本行處理,進入下一行處理。
實例1:
[root@localhost ~]# cat next.txt
aa bb
cc dd
ee ff
gg hh
[root@localhost ~]# awk '{if(NR == 1) next;print $1,$2}' next.txt
cc dd
ee ff
gg hh