2017-03-15 76 views
1

我有一個包含一個小的測試文件:awk中具有可變和否定多個搜索字詞

awk this 
and not awk this 
but awk this 
so do awk this 

我累了,下面的awk命令,在bash,但每次不產生輸出:

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中產生「event not found」錯誤。

如何搜索變量並在相同的命令中否定另一個字符串?

回答

2

使用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 
+1

謝謝!我認爲這會做到。 –

0

AWK點,呆呆地看着我和下面的工作只是罰款:

awk -vf=awk '$0 ~ f && !/not/' file 
+0

這裏沒有特定的Gawk。這似乎是Anubhava現有答案的重新調整。 – tripleee