awk - Variable expansion in regex

北城以北 提交于 2021-02-04 19:39:19

问题


Why doesn't something like this work:

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

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


回答1:


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


来源:https://stackoverflow.com/questions/19188617/awk-variable-expansion-in-regex

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!