awk-正则表达式中的变量扩展 [英] awk - Variable expansion in regex

查看:73
本文介绍了awk-正则表达式中的变量扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么这样的工作不起作用:

Why doesn't something like this work:

echo 4 | awk --assign=abc=4 '/$abc/'

实际示例要复杂得多.基本上我有一个正则表达式,我需要重复几次,所以我将其存储在 abc 中.有什么方法可以在/< regex>/中扩展awk变量吗?我尝试过单引号和双引号,每种组合.我真的需要将该行用单引号引起来,因为我有几个双引号,它实际上更像是 awk --assign = test = something'/$ test/{这里很多东西,包括$ test多次使用double引号;}'

The actual example is much more complicated. Basically I have a regex I need repeated several times so I'm storing it in abc. Is there any way to expand an awk variable in /<regex>/? I've tried single and double quotes, every combination. I really need that line to be single quoted because I have several double quotes, it actually looks more like awk --assign=test=something '/$test/ { a lot of stuff here inc $test several times with double quotes; }'

推荐答案

awk在//正则表达式之间不使用变量:

awk doesn't use variables between / and / regex:

使用正则表达式运算符,以下等效项将具有相同的效果:

Following equivalent will work with same effect using ~ regex operator:

echo 4 | awk --assign=abc=4 '$0 ~ abc'
4

这篇关于awk-正则表达式中的变量扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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