简体   繁体   English

AWK正则表达式不起作用

[英]Awk regexp not working

I have a very simple awk script that is supposed to extrapolate some log lines with a regex. 我有一个非常简单的awk脚本,应该使用正则表达式来推断一些日志行。 I'm testing it but if I do this: 我正在测试,但是如果我这样做:

awk -F ' ' '$5 ~ /MyClassName/ {p=1} p {print $5}' myLogFile.log

I get lines from other classes even if the output should be only lines containing MyClassName. 即使输出仅是包含MyClassName的行,我也可以从其他类得到行。

Can anyone explain me what's wrong with this? 谁能解释这是怎么回事?

Thank you. 谢谢。

You can avoid using p altogether and AFAIK setting FS ( -F ) to a literal space is the same as not setting it as all: 您可以避免一起使用p并且AFAIK将FS-F )设置为文字空间与未将其全部设置为相同:

awk '$5 ~ /MyClassName/ {print $5}' myLogFile.log

And since you are only interested in column 5 you should be able to use: 并且由于您仅对第5列感兴趣,因此您应该可以使用:

awk '{$0=$5} /MyClassName/' myLogFile.log

您永远不会将p重置为零。

awk -F ' ' '{p=0} $5 ~ /MyClassName/ {p=1} p {print $5}' myLogFile.log

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM