一、if 结构
1.单条语句
语法:if(conditional-expression )
actionif 是关键字
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 结构
在 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 循环
语法:while (codition)
Actionswhile 是 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
三、do-while 循环
While 循环是一种进入控制判断的循环结构,因为在进入循环体前执行判断。而 do-while 循 环是一种退出控制循环,在退出循环前执行判断。 do-while 循环至少会执行一次,如果条 件为 true,它将一直执行下去。
语法:do
action
while(condition)
实例1:[root@localhost ~]# awk 'BEGIN{ do print "this is a test";while (count++
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循环
语法: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 语句
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 语句跳过后面剩余的循环部分,立即进入下次循环。 请注意, 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 命令立即停止脚本的运行,并忽略脚本中其余的命令。 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 语句
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
八、switch语句
语法:switch (expression) {
case value or regular expression:
case-body
default:
default-body}
说明:switch语句gawk默认并不支持,需要编译安装gawk时使用--enable-switch
选项开启此功能。