如何使用`awk`打印正则表达式匹配项? [英] How to print regexp matches using `awk`?

查看:247
本文介绍了如何使用`awk`打印正则表达式匹配项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种方法可以在shell中使用awk命令打印正则表达式匹配项(但仅匹配字符串)?

是的,在awk中使用match()函数,并为其提供可选的数组参数(在我的示例中为a).当您执行此操作时,第0个元素将是与正则表达式匹配的部分

$ echo "blah foo123bar blah" | awk '{match($2,"[a-z]+[0-9]+",a)}END{print a[0]}'
foo123

Is there a way to print a regexp match (but only the matching string) using awk command in shell?

解决方案

Yes, in awk use the match() function and give it the optional array parameter (a in my example). When you do this, the 0-th element will be the part that matched the regex

$ echo "blah foo123bar blah" | awk '{match($2,"[a-z]+[0-9]+",a)}END{print a[0]}'
foo123

这篇关于如何使用`awk`打印正则表达式匹配项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆