从内部AWK使用bash变量 [英] Use bash variable from within AWK
本文介绍了从内部AWK使用bash变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我刚才想这从我的shell脚本,并没有如预期的结果。
I have just tried this from my shell script and result wasn't as intended
REF=SEARCH_TEXT
echo "some text" | awk '/$REF/{print $2}'
它没找到$ REF即使文本包含它
It's didn't find $REF even with text contained it
推荐!
推荐答案
您的问题是措辞的真的差...
You question is worded really poor...
不管怎样,我想你想要这样的:
Anyway, I think you want this:
REF=SEARCH_TEXT
echo "some text" | awk "/$REF/{print \$2}"
的注意 $ 2
的转义和双引号。的
Note the escaping of $2
and the double quotes.
或本
REF=SEARCH_TEXT
echo "some text" | awk "/$REF/"'{print $2}'
的注意合理使用双引号和单引号,并没有逃脱的 $ 2
。的
您必须使用shell扩展,否则将包括出口shell变量,并使用它与awk的环境 - 这是矫枉过正的这种情况:
You have to use shell expansion, as otherwise it would encompass exporting a shell variable and using it from the environment with awk - which is overkill in this situation:
export REF=SEARCH_TEXT
echo "some text" | awk '{if (match($0, ENVIRON["REF"])) print $2}'
我觉得awk不支持在/.../警卫变量。 请纠正我,如果我错了。的
这篇关于从内部AWK使用bash变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文