Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 带变量和否定的Awk多个搜索项_Bash_Variables_Awk_Negate - Fatal编程技术网

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

谢谢!我想这就行了。这里没有什么特别的傻眼。这似乎是对阿努巴瓦现有答案的一个调整过的重述。