Linux高级文本处理之gawk分支和循环(四)推荐

一、if 结构

1.单条语句

语法:

if(conditional-expression)action

if 是关键字

conditional-expression 是要检测的条件表达式

action 是要执行的语句

2.多条语句

如果要执行多条语句,需要把他们放在{ } 中,每个语句之间必须用分号或换行符分开,如下所示.

语法:

if(conditional-expression)action1;action2;}

如果条件为真,{ } 中的语句会依次执行。当所有语句执行完后,awk 会继续执行后面的语句。

注意:与具体的花括号后面不能加分号。

实例1:打印数量小于等于 5 的所有商品

[root@localhost~]#!catcatitems.txt101,HDCamcorder,Video,210,10102,Refrigerator,Appliance,850,2103,MP3Player,Audio,270,15104,TennisRacket,Sports,190,20105,LaserPrinter,Office,475,5[root@localhost~]#awk-F,'{if($5 =5)print"Only",$5,"qtyof",$2"isavailable"}'items.txtOnly2qtyofRefrigeratorisavailableOnly5qtyofLaserPrinterisavailable

实例2:打印价钱在 500 至 100,并且总数不超过 5 的商品

[root@localhost~]#awk-F,'{if(($4 =500 $4 =1000) ($5 =5))print"Only",$5,"qtyof",$2,"isavailable"}'items.txtOnly2qtyofRefrigeratorisavailable

二、if else 结构

在 if else 结构中, 还可以指定判断条件为 false 时要执行的语句。 下面的语法中,如果条件 为 true,那么执行 action1,如果条件为 false,则执行 action2

语法:

if(conditional-expression)action1action2

此外, awk 还有个条件操作符( ? : ), 和 C 语言的三元操作符等价。

和 if-else 结构相同,如果 codintional-expresion 是 true,执行 action1,否则执行 action2。

三元操作符:

codintional-expression?action1:action2;

实例1:如果商品数量不大于 5,打印”Buy More”,否则打印商品数量

[root@localhost~]#catif.awkBEGIN{FS=",";}{if($5 =5)print"BuyMore:Order",$2,"immediately!"print"ShellMore:Givediscounton",$2,"immediately!"[root@localhost~]#catitems.txt101,HDCamcorder,Video,210,10102,Refrigerator,Appliance,850,2103,MP3Player,Audio,270,15104,TennisRacket,Sports,190,20105,LaserPrinter,Office,475,5[root@localhost~]#awk-fif.awkitems.txtShellMore:GivediscountonHDCamcorderimmediately!BuyMore:OrderRefrigeratorimmediately!ShellMore:GivediscountonMP3Playerimmediately!ShellMore:GivediscountonTennisRacketimmediately!BuyMore:OrderLaserPrinterimmediately!

实例2:用三元操作符,把 items.txt 文件中的每两行都以逗号分隔合并起来

[root@localhost~]#catitems.txt101,HDCamcorder,Video,210,10102,Refrigerator,Appliance,850,2103,MP3Player,Audio,270,15104,TennisRacket,Sports,190,20105,LaserPrinter,Office,475,5[root@localhost~]#awk'ORS=NR%2?",":"\n"'items.txt101,HDCamcorder,Video,210,10,102,Refrigerator,Appliance,850,2103,MP3Player,Audio,270,15,104,TennisRacket,Sports,190,20105,LaserPrinter,Office,475,5,[root@localhost~]#

相同于:

[root@localhost~]#awk'{if(NR%2==0)ORS="\n";elseORS=",";print}'items.txt101,HDCamcorder,Video,210,10,102,Refrigerator,Appliance,850,2103,MP3Player,Audio,270,15,104,TennisRacket,Sports,190,20105,LaserPrinter,Office,475,5,[root@localhost~]#

二、while 循环

语法:

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";printstring}'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

实例2:计算 items-sold.txt 文件中,每件商品出售的总数

[root@localhost~]#catitems-sold.txt101210581012102014302103106112051310423406510510257126[root@localhost~]#awk' {i=2;total=0;while(i =NF) {total=total+$i;i++;} printtotal}'items-sold.txt42

三、do-while 循环

While 循环是一种进入控制判断的循环结构,因为在进入循环体前执行判断。而 do-while 循 环是一种退出控制循环,在退出循环前执行判断。 do-while 循环至少会执行一次,如果条 件为 true,它将一直执行下去。

语法:

doactionwhile(condition)

实例1:

[root@localhost~]#awk'BEGIN{doprint"thisisatest";while(count++ 5);}'thisisatestthisisatestthisisatestthisisatestthisisatestthisisatest

注意:首先执行一次,所以上述实例打印6次。

实例2:

[root@localhost~]#catdowhile.awktotal=0;do{total=total+$i;while(i =NF)print"Item",$1,":",total,"quantitiessold";[root@localhost~]#awk-fdowhile.awkitems-sold.txtItem101:47quantitiessoldItem102:10quantitiessoldItem103:65quantitiessoldItem104:20quantitiessoldItem105:42quantitiessold

四、for循环

语法:

for(initialization;condition;increment/decrement)

for 循环一开始就执行 initialization,然后检查 condition,如果 condition 为 true,执行 actions,然后执行 increment 或 decrement.如果 condition 为 true,就会一直重复执行 actions 和increment/decrement。

实例1:计算1到4的整数和。

[root@localhost~]#echo"1234"|awk'{for(i=1;i i++)total+=$i;}END{printtotal}'10

实例2:把文件中的字段反序打印出来

[root@localhost~]#awk' BEGIN{ORS=""} {for(i=NF;i i--) print$i,""; print"\n"}'items-sold.txt121085102101203410102135201161010356043210461275210105

注意:在 awk 里, print 和 printf 的区别是, print 输出当前记录(行 record), 并在最后自动加上 ORS ( 默认是\n ,但可以是任何字符串). 而 printf 是按给定格式输出内容, 你给它 ORS 参数, 它就输出 ORS, 没给就不输出.

五、break 语句

Break 语句用来跳出它所在的最内层的循环(while,do-while,或 for 循环)。请注意, break 语句 只有在循环中才能使用。

实例1:打印某个月销售量为 0 的任何商品

[root@localhost~]#awk' {for(i=2;i i++) {if($i==0) {print$0;break;}}}'items-sold.txt102014302104234065

实例2:

[root@localhost~]#awk'BEGIN{while(1){i++;if(i==10)break;print"young";}}'youngyoungyoungyoungyoungyoungyoungyoungyoung

六、 continue 语句

Continue语句跳过后面剩余的循环部分,立即进入下次循环。请注意,continue只能用在循环当中。

实例1:打印 items-sold.txt 文件中所有商品的总销售量

[root@localhost~]#awk' {total=0;for(i=1;i i++) {if(i==1)continue;total+=$i}; printtotal}'items-sold.txt42

实例2:

[root@localhost~]#awk'BEGIN{for(i=1;i i++) {if(i==3)continue; print"thisis:",i,"x";}}'thisis:1xthisis:2xthisis:4xthisis:5x

六、exit 语句

exit 命令立即停止脚本的运行,并忽略脚本中其余的命令。 exit 命令接受一个数字参数最为 awk 的退出状态码,如果不提供参数,默认的状态码是 0.

实例1:

[root@localhost~]#catitems-sold.txt101210581012102014302103106112051310423406510510257126[root@localhost~]#catsoi=2;total=0;while(i++ =NF)if($i==0){#某月份销量为0则退出脚本print"Item",$1,"hadamonthwithnoitemsold";exit;[root@localhost~]#awk-fsoitems-sold.txtItem102hadamonthwithnoitemsold

七、next 语句

next语句表示提前结束对本行文本的处理,二提前进入下一行的处理。

实例1:

[root@localhost~]#catnext.txtaabbccddeeffgghh[root@localhost~]#awk'{if(NR==1)next;print$1,$2}'next.txtccddeeffgghh

八、switch语句

语法:

switch(expression){casevalueorregularexpression:case-bodydefault:default-body}

说明:switch语句gawk默认并不支持,需要编译安装gawk时使用

--enable-switch

选项开启此功能。

快乐要懂得分享,才能加倍的快乐

Linux高级文本处理之gawk分支和循环(四)推荐

相关文章:

你感兴趣的文章:

标签云: