该工具awk
具有线模式匹配,如
/pattern/ { statements; }
有没有办法将字符串pattern
作为变量,用于match
表达式等?
或者更好,直接获取:
(groups)
模式中有任何匹配组在中{statements}
块?
如果您使用 GNU awk,而不是/pattern/
在条件部分使用,则使用match
它的第三个参数,match(string, regexp [, array])
您可以访问匹配的文本、起始索引、长度和组:
$ echo foobar |
gawk 'match($0, /(fo*)(b.*)/, a) {
print a[0],a[0,"start"],a[0,"length"] # 0 index refers to whole matched text
print a[2],a[2,"start"],a[2,"length"] # 1, 2, etc. to matched groups
}'
foobar 1 6
bar 4 3
有关更多信息,请参阅GNU awk 文档match
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句