Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grep - List all lines not containing both pattern

Tags:

grep

bash

I have a text file having some records. I have two patterns to verify and I want to list all lines from the file not containing both pattern. How can I do this using grep command?
I tried few things using grep -v but nothing seem to work.

Suppose my text file is as follows.
1. qwerpattern1yui
2. adspattern2asd
3. cczxczc
4. jkjkpattern2adsdapattern1

I want to list lines 1, 2 and 3 only.

Thanks in advance.

like image 807
Newbie Avatar asked Dec 05 '22 07:12

Newbie


2 Answers

You can use:

grep -w -v -e "word1" -e "word2" file

OR else using egrep:

egrep -w -v -e "word1|word2" file

UPDATE: Based on comments, it seems following awk will work better:

awk '!(/pattern1/ && /pattern2/)' file
like image 184
anubhava Avatar answered Jan 09 '23 02:01

anubhava


If I'm keeping up with the comments and edits right, I think this is what you need:

$ grep -E -v 'pattern1.*pattern2|pattern2.*pattern1' test
1. qwerpattern1yui
2. adspattern2asd
3. cczxczc
$ 
like image 20
Digital Trauma Avatar answered Jan 09 '23 03:01

Digital Trauma