“grep 字符串 | grep 字符串”与 awk 没有管道

Rid*_*his 5 bash awk

有没有办法做到:

output | grep "string1" | grep "string2" 
Run Code Online (Sandbox Code Playgroud)

但是使用awk,没有管道?

就像是:

output | awk '/string1/ | /string2/ {print $XY}'
Run Code Online (Sandbox Code Playgroud)

如果有意义,结果应该是匹配的子集。

Ste*_*ris 11

with的默认操作awk是打印,所以相当于

output | grep string1 | grep string2
Run Code Online (Sandbox Code Playgroud)

output | awk '/string1/ && /string2/'
Run Code Online (Sandbox Code Playgroud)

例如

$ cat tst
foo
bar
foobar
barfoo
foothisbarbaz
otherstuff

$ cat tst | awk '/foo/ && /bar/'
foobar
barfoo
foothisbarbaz
Run Code Online (Sandbox Code Playgroud)


ter*_*don 5

如果要awk以任何顺序查找与string1 都匹配的行string2,请使用&&

 output | awk '/string1/ && /string2/ {print $XY}'
Run Code Online (Sandbox Code Playgroud)

如果您想匹配其中一个string1string2(或两者),请使用||

 output | awk '/string1/ || /string2/ {print $XY}'
Run Code Online (Sandbox Code Playgroud)

  • 我认为 `{print $XY}` 东西就像一个占位符,OP 认为它是有用/必要的。我相信答案更好,因为它没有多大意义。 (2认同)