简体   繁体   English

awk 如何打印它映射的正则表达式?

[英]how awk print the regex it maps?

Here is the file contents:这是文件内容:

# cat text 
16:10:29 DEBUG MY_Output:90 1 5de0d275c2f55: send response

As I do regex map with awk :当我用awk做正则表达式映射时:

# cat text | awk '{if($0~/[0-9]{2}:[0-9]{2}:[0-9]{2}.*/) print $0}'
(print nothing)

# cat text | awk '/[0-9]{2}:[0-9]{2}:[0-9]{2}.*/ {print $0}'
(print nothing)

# cat text | awk '/[0-9]{2}:[0-9]{2}:[0-9]{2}.*/1'
16:10:29 DEBUG MY_Output:90 1 5de0d275c2f55: send response

My Question is: why {if($0~/[0-9]{2}:[0-9]{2}:[0-9]{2}.*/) print $0} , /[0-9]{2}:[0-9]{2}:[0-9]{2}.*/ {print $0} print nothing, but /[0-9]{2}:[0-9]{2}:[0-9]{2}.*/1 print the result.我的问题是:为什么{if($0~/[0-9]{2}:[0-9]{2}:[0-9]{2}.*/) print $0} , /[0-9]{2}:[0-9]{2}:[0-9]{2}.*/ {print $0}什么都不打印,但是/[0-9]{2}:[0-9]{2}:[0-9]{2}.*/1打印结果。

As I expected, the three expressions play the same meaning, as [1] describes, 1 , {print} , {print $0} do the same thing in action.正如我所料,这三个表达式具有相同的含义,正如[1]所描述的那样, 1{print}{print $0}执行相同的操作。

Also the another experiment likely to check:还有另一个可能检查的实验:

# awk '{if(/[0-9]{2}:[0-9]{2}:[0-9]{2}/) print $0}' <<< "16:10:29" 
(print nothing)

# awk '/[0-9]{2}:[0-9]{2}:[0-9]{2}/{print $0}' <<< "16:10:29"
(print nothing)

# awk '/[0-9]{2}:[0-9]{2}:[0-9]{2}/1' <<< "16:10:29"
16:10:29

Thank you very much.非常感谢。

After knowing OP's awk version(in comments section) looks like it it OLD one so [0-9]{2} is NOT supported in it(I believe so), so can you try following command once.在知道 OP 的awk版本(在评论部分)之后,它看起来像是旧版本,所以它不支持[0-9]{2} (我相信是这样),所以你可以尝试一次以下命令。

awk '/[0-9][0-9]:[0-9][0-9]:[0-9][0-9]/' Input_file

This should work with your provided awk version.这应该适用于您提供的awk版本。



Also with old version of awk you could use --re-interval to get it worked, I don't have that version with me so couldn't test it.此外,对于旧版本的awk您可以使用--re-interval使其工作,我没有那个版本,因此无法对其进行测试。

awk --re-interval '/[0-9]{2}:[0-9]{2}:[0-9]{2}/'  Input_file

In older versions of awk to invoke EREs we need to include --re-interval with awk codes, hence it is NOT working with normal awk code.在旧版本的awk ,我们需要在awk代码中包含--re-interval来调用 ERE,因此它--re-interval用于普通的awk代码。

NOTE: In new versions of gawk s --re-interval is depreciated since OP has old version of awk so have mentioned it in solution.注意:在新版本的gawk --re-interval已贬值,因为 OP 具​​有旧版本的awk因此在解决方案中提到了它。 Adding cross site reference link https://unix.stackexchange.com/questions/354553/awk-repetition-n-is-not-working as per OP's comments too here.根据 OP 的评论,添加跨站点参考链接https://unix.stackexchange.com/questions/354553/awk-repetition-n-is-not-working

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

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