1.if语句
awk
{if (expression){
action;....
}
else if(expression){
action;.....
}
else if(expression){
action;.....
}
else{
action;....
}
}
------------------------------------------------------------------------------
-bash-3.2$ cat chengji
zhangsan 80
lisi 81.5
wangwu 93
zhangsan 85
lisi 828
wangwu 97
zhangsan 90
lisi 92
wangwu 881
zhangsan 80
lisi 81.5
wangwu 93
zhangsan 85
lisi 88
wangwu 97
zhangsan 90
lisi 92
wangwu 883
-bash-3.2$ awk '{a[NR]=$0}{if($0~/wan/)print "yangkai";else if($0~/zhang/)print}' chengji
zhangsan 80
yangkai
zhangsan 85
yangkai
zhangsan 90
yangkai
zhangsan 80
yangkai
zhangsan 85
yangkai
zhangsan 90
yangkai
-bash-3.2$ awk '{a[NR]=$0}{if($0~/wan/){print "yangkai"}else if($0~/zhang/){print}}' chengji
zhangsan 80
yangkai
zhangsan 85
yangkai
zhangsan 90
yangkai
zhangsan 80
yangkai
zhangsan 85
yangkai
zhangsan 90
yangkai
-bash-3.2$ awk '{a[NR]=$0}{if($0~/wan/){print "yangkai"}else if($0~/zhang/){print}else{print "pangpang"}}' chengji
zhangsan 80
pangpang
yangkai
zhangsan 85
pangpang
yangkai
zhangsan 90
pangpang
yangkai
zhangsan 80
pangpang
yangkai
zhangsan 85
pangpang
yangkai
zhangsan 90
pangpang
yangkai
-bash-3.2$ //草,那会弄了半天不知道为啥不对,回来发现print 后面的pangpang忘记加""了。。。没有语法提示,悲剧。。。
-bash-3.2$ awk '{a[NR]=$0}END{if($0~/wan/){print "yangkai"}else if($0~/zhang/){print}else{print "pangpang"}}' chengji
yangkai
-bash-3.2$
-bash-3.2$ awk '{a[NR]=$0}{if(a[NR]~/wan/)print}' chengji
wangwu 93
wangwu 97
wangwu 881
wangwu 93
wangwu 97
wangwu 883
-bash-3.2$//废话了这条
阅读(6744) | 评论(0) | 转发(0) |