awk:有什么方法可以访问匹配的组? [英] awk: any way to access the matched groups in action?

查看:87
本文介绍了awk:有什么方法可以访问匹配的组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常发现自己在动作上与模式相同,以访问输入记录的某些部分,例如

I often find myself doing the same match in the action as the pattern, to access some part of the input record, e.g.

/^Compiled from \"(.*)\"$/ {
    file_name = gensub("^Compiled from \"(.*)\"$", "\\1", "g");
    print file_name;
}

因此,regexp匹配完成了两次.有什么方法可以在不再次匹配的情况下访问操作中的\\1?

So the regexp matching is done twice. Is there any way I can access \\1 in the action without matching again?

我正试图减少模式匹配和额外的代码.

I am trying to both reduce on pattert matching and extra code.

推荐答案

不幸的是,GAWK没有使用空//sed的结转功能.

Unfortunately, GAWK, doesn't have the carry-forward feature of sed which uses an empty //.

sed '/\(patt\)ern/ {s//new\1/}' inputfile

但是,由于变量是最近被发明的,因此您可以为之高兴!

However, you can rejoice since variables have recently been invented and they can be used for just this purpose!

BEGIN {
    pattern = "^Compiled from \"(.*)\"$"
}
$0 ~ pattern {
    file_name = gensub(pattern, "\\1", "");
    print file_name;
}

这篇关于awk:有什么方法可以访问匹配的组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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