Is there a way to do:
output | grep "string1" | grep "string2"
BUT with awk, WITHOUT PIPE?
Something like:
output | awk '/string1/ | /string2/ {print $XY}'
Result should be subset of matches, if tha makes sense.
Is there a way to do:
output | grep "string1" | grep "string2"
BUT with awk, WITHOUT PIPE?
Something like:
output | awk '/string1/ | /string2/ {print $XY}'
Result should be subset of matches, if tha makes sense.
The default action with awk
is to print, so the equivalent of
output | grep string1 | grep string2
is
output | awk '/string1/ && /string2/'
e.g.
$ cat tst
foo
bar
foobar
barfoo
foothisbarbaz
otherstuff
$ cat tst | awk '/foo/ && /bar/'
foobar
barfoo
foothisbarbaz
awk 'index($0,"string1") && index($0,"string2")
– Ed Morton
Jan 02 '21 at 18:31
If you want awk
to find lines that match both string1
and string2
, in any order, use &&
:
output | awk '/string1/ && /string2/ {print $XY}'
If you want to match either string1
or string2
(or both), use ||
:
output | awk '/string1/ || /string2/ {print $XY}'
{print $XY}
thing is something like a placeholder that the OP thought would be useful/necessary. I believe it is better out of the answer since it doesn't make much sense.
– Quasímodo
Dec 30 '20 at 15:34
|
missing :awk '/string1/ || /string2/ {print $XY}'
– Archemar Dec 30 '20 at 14:50||
will match any line with either “string1” or “string2”, whereas thegrep
s only match lines with both (so&&
in AWK). – Stephen Kitt Dec 30 '20 at 14:56grep "string1" | grep "string2"
does not grep for 2 strings, it greps for 2 regexps.You'd have to add-F
to your grep commands to grep for 2 strings and then the awk answer you accepted would be wrong. – Ed Morton Jan 02 '21 at 18:30