任何方式来访问运行中的匹配组? [英] any way to access the matched groups in action?

查看:110
本文介绍了任何方式来访问运行中的匹配组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常发现自己做同样的比赛中的行动模式,进入输入记录,例如某些部分

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;
}

所以,正则表达式匹配两次完成。有没有什么办法可以访问 \\\\ 1 中,而无需再次匹配的行动?

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

我想无论在pattert匹配与额外的code减少。

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;
}

这篇关于任何方式来访问运行中的匹配组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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