Awk基本入门[4] Awk Conditional Statements and Loops

1、Simple If Statement

语法:

if (conditional-expression)
action

如果是多个action,则语法如下:

if (conditional-expression)
{
action1;
action2;
}

Print all the items with quantity <=5:

$ awk -F "," '{ if ($5 <= 5) \
print "Only",$5,"qty of",$2, "is available"; }' \
items.txt
Only 2 qty of Refrigerator is available
Only 5 qty of Laser Printer is available
2、If Else Statement

语法:

if (conditional-expression)
    action1
else
    action2

该语法与三元运算符类似:

conditional-expression ? action1 : action2 ;

The following example displays the message "Buy More" when the total quantity is <= 5, and prints "Sell More" when the total quantity is not <=5.

$ cat if-else.awk
BEGIN {
FS=",";
}
{
if ( $5 <= 5 )
print "Buy More: Order", $2, "immediately!"
else
print "Sell More: Give discount on", $2,"immediately!"
}
$ awk -f if-else.awk items.txt
Sell More: Give discount on HD Camcorder immediately!
Buy More: Order Refrigerator immediately!
Sell More: Give discount on MP3 Player immediately!
Sell More: Give discount on Tennis Racket immediately!
Buy More: Order Laser Printer immediately!
3、While Loop

语法:

while(condition)
    actions

示例:打印50个字符“x”

$ awk 'BEGIN \
{ while (count++<50) string=string "x"; print string }'
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
4、Do-While Loop

语法:

do
    action
while(condition)

The following awk program prints the total number of quantities sold from the items-sold.txt file for each item. The output of this program is exactly the same as the while.awk program, but this uses do-while.

$ cat dowhile.awk
{
i=2; total=0;
do
{
total = total + $i;
i++;
} while (i <= NF)
print "Item", $1, ":", total, "quantities sold";
}
$ 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
5、For Loop Statement

语法:

for(initialization;condition;increment/decrement)
    actions

The following example prints the sum of fields in a line. Initially the variable i is initialized to 1; if i is less than or equal to the total number of fields, the  urrent field is added to the total; I is incremented and the test is repeated.

$ echo "1 2 3 4" | awk \
'{ for (i = 1; i <= NF; i++) total = total+$i }; \
END { print total }'
10
6、Break Statement

The break statement is used for jumping out of the innermost loop (while, do-while, or for loop) that encloses it. Please note that the break statement has meaning only if you use it with in the loop.

The following example prints any item number that has a month with no sold items, i.e. that has 0 for any one of the values field2 through field7.

$ cat break.awk
{
  i=2; total=0;
  while (i++ <= NF)
    if ($i == 0) {
      print "Item", $1, "had a month with no item sold"
      break;
    }
}
$ awk -f break.awk items-sold.txt
Item 102 had a month with no item sold
Item 104 had a month with no item sold
7、Continue Statement

The continue statement skips over the rest of the loop body causing the next cycle around the loop to begin immediately. Please note that the continue statement has meaning only if you use it with in the loop.

The following awk script prints the value of x at each iteration except the 5th, where a continue statement skips the printing.

$ awk 'BEGIN{
    x=1;
    while(x<=10)
    {
      if(x==5){
        x++;
        continue;
      }
    print "Value of x",x;x++;
    }
}'

The above command produces the following output.

Value of x 1
Value of x 2
Value of x 3
Value of x 4
Value of x 6
Value of x 7
Value of x 8
Value of x 9
Value of x 10
8、Exit Statement

The exit statement causes the script to immediately stop executing the current commands, and also ignores the remaining lines from the input file.

The following example prints the first item number that has a month when no items were sold. This is similar to the break.awk example, except that it exits  when it finds a month with no sales for an item, rather than going on to look at the other items.

$ cat exit.awk
{
  i=2; total=0;
  while (i++ <= NF)
    if ($i == 0) {
      print "Item", $1, "had a month with no item sold"
      exit;
    }
}
$ awk -f exit.awk items-sold.txt
Item 102 had a month with no item sold

Note: Item 104 also had a month with no item sold. But, it was not displayed above, as we used exit in the while loop.

 

 
 
 
 
 
 

转载于:https://www.cnblogs.com/yangfengtao/p/3161443.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值