1

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

2
  • line 4 does not contain failed, expecting it to match doesn't fit your description. Commented Jun 5, 2013 at 17:18
  • I'm assuming these are not your actual words. How would you handle e.g. searching for "bash" and "shell" if the line contained "bashell"? Match because the line contains both substrings? Fail because the words shouldn't match parts of other words? Fail because the strings should not overlap? All the answers currently given take the first approach. Commented Jun 6, 2013 at 1:16

5 Answers 5

5

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.

Sign up to request clarification or add additional context in comments.

Comments

3

first of all, I think line 4 should not be in result. there is no failed in it.

grep 'succeeded' input|grep 'failed' 

Comments

2

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".

Comments

1

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.

Comments

0
grep 'failed' <(grep 'succeeded' inputFile) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.