Bash 带变量和否定的Awk多个搜索项
我有一个小测试文件,包含:Bash 带变量和否定的Awk多个搜索项,bash,variables,awk,negate,Bash,Variables,Awk,Negate,我有一个小测试文件,包含: awk this and not awk this but awk this so do awk this 我在bash中尝试了以下awk命令,但每个命令都不产生输出: f=awk; awk '/$f/ && !/not/' test.txt f=awk; awk '/\$f/ && !/not/' test.txt f=awk; awk '/"$f"/ && !/not/' test.txt f=a
awk this
and not awk this
but awk this
so do awk this
我在bash中尝试了以下awk命令,但每个命令都不产生输出:
f=awk; awk '/$f/ && !/not/' test.txt
f=awk; awk '/\$f/ && !/not/' test.txt
f=awk; awk '/"$f"/ && !/not/' test.txt
f=awk; awk -v f="$f" '/f/ && !/not/' gtest.txt
使用双引号“
会在shell中由于!
而产生“未找到事件”错误
如何在同一命令中搜索变量并对另一个字符串求反?使用
awk
如下:
f='awk'
awk -v f="$f" -v n='not' '$0 ~ f && $0 !~ n' file
awk this
but awk this
so do awk this
或者如果您不想将n='not'
传递给awk
:
awk -v f="$f" '$0 ~ f && $0 !~ /not/' file
awk this
but awk this
so do awk this
awk指向我的gawk,下面的工作很好:
awk -vf=awk '$0 ~ f && !/not/' file
谢谢!我想这就行了。这里没有什么特别的傻眼。这似乎是对阿努巴瓦现有答案的一个调整过的重述。