免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4682 | 回复: 7
打印 上一主题 下一主题

awk 里面只有 if 没有 else? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2004-05-09 09:57 |只看该作者 |倒序浏览
aaa.log的部分文件内容

CPUSTATE   STANDBYSIDE   SBWORKSTATE
NORMAL      CPUB                WORKING


我想让awk检查我的log文件 测试 CPUSTATE 下面一行匹配 特定关键字 就输出normal 不匹配 就输出abnormal

但是翻了下我手头的资料,没有发现awk的else的介绍, man awk 后发现虽然有else,但是怎么也用不对, 请高人们指点指点



workstat = `cat aaa.log| awk '/CPUSTATE/{getline;if($1 = "NORMAL" && $2 = "CPUB" && $3 = "WORKING"print "normal" else print "abnormal" }'`

论坛徽章:
0
2 [报告]
发表于 2004-05-09 11:06 |只看该作者

awk 里面只有 if 没有 else?

print "normal";

论坛徽章:
0
3 [报告]
发表于 2004-05-09 11:36 |只看该作者

awk 里面只有 if 没有 else?

还有问题:
判断=应该为==

论坛徽章:
0
4 [报告]
发表于 2004-05-09 12:34 |只看该作者

awk 里面只有 if 没有 else?

哦,我正好刚刚解决,就是把改成==了,谢谢楼上2位了

论坛徽章:
0
5 [报告]
发表于 2004-05-13 10:55 |只看该作者

awk 里面只有 if 没有 else?

here I also need to do some operation according to the different condition in awk command line.

For example:
if ... some operation
elif... some opertaion
else... some operation

In fact I have the same issue as the above gentlemen, that is to say if the condition is ready some operation is required, otherwise the other operation is executed.

Any other suggestion or comments pls let me know.

Thanks.

论坛徽章:
0
6 [报告]
发表于 2004-05-14 14:21 |只看该作者

awk 里面只有 if 没有 else?

哪位大虾能说说?谢谢了!

论坛徽章:
0
7 [报告]
发表于 2004-05-14 14:29 |只看该作者

awk 里面只有 if 没有 else?

Here I find some description about the control flow in awk statement as following:
2、流程控制语句
awk提供了完备的流程控制语句,其用法与C语言类似。下面我们一一加以说明:

if...else语句:

格式:
if(表达式)
语句1
else
语句2

格式中"语句1"可以是多个语句,如果你为了方便awk判断也方便你自已阅读,你最好将多个语句
用{}括起来。
awk分枝结构允许嵌套,其格式为
if(表达式1)
{if(表达式2)
语句1
else
语句2
}
语句3
else {if(表达式3)
语句4
else
语句5
}
语句6
当然实际操作过程中你可能不会用到如此复杂的分枝结构,这里只是为了给出其样式罢了。

here I do not do any test for the above sentences.

Good luck to you, young guys.

Thanks!

论坛徽章:
0
8 [报告]
发表于 2004-05-14 15:39 |只看该作者

awk 里面只有 if 没有 else?

Hi all,

Here I have done some test with the if...else control flow used in awk command script and find that it can work well.

The following is the test example for you to reference:

awk '{if($2 != 2){print $0} else { if($10 == "220.192.144.129" && $3 !~/^46003/) {$NF=3; print $0} else { if($10 !~/220\.192\./) { $NF=4; print $0} else {print $0}}}}' udrinput > udroutput2
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP