Reputation: 142
how to do a grep with two parameter (operation AND); that find all lines that containt the word succeeded and failed exactly.
EG:
1. succeeded, failed
2. failed, succeeded
3. failed,failed
4. succeeded, succeeded
5. failed
6. faileds
7. succeeded
I need that search return the lines that containt words "succeeded" and "failed", as line 1, 2 and 4
Upvotes: 1
Views: 6871
Reputation: 246807
A couple of different approaches that only require a single pass through the file:
awk '/succeeded/ && /failed/' file
sed -n '/succeeded/ {/failed/ p}' file
perl -ne 'print if /succeeded/ and /failed/' file
I was hoping grep -e succeeded -e failed file
would work, but it returns "one or the other" instead of "both".
Upvotes: 2
Reputation: 10516
To grep for 2 words existing on the same line with AND
grep succeeded FILE | grep failed
grep succeeded FILE
will print all lines that have succeedded in them from FILE, and then grep failed
will print the lines that have failed in them from the result of grep succeeded FILE
. Hence, if you combine these using a pipe, it will show lines containing both succeeded and failed.
Upvotes: 1
Reputation: 58808
Very simple:
grep succeeded | grep failed
Alternatively:
grep 'succeeded.*failed\|failed.*succeeded'
The latter approach doesn't scale very well (you end up with P(N, N)
subclauses with N
words), but can be more flexible when handling things like relative position between the words.
Upvotes: 5
Reputation: 195059
first of all, I think line 4 should not be in result. there is no failed
in it.
grep 'succeeded' input|grep 'failed'
Upvotes: 3