0

I want to find which of the patterns or words are not present in file. Example :

test.txt

marco 
polo
charlie 
anthony
john

egrep 'marco|polo' test.txt will output marco and polo

But I want a command to output the word if its not present in file:

Ex : egrep 'cindrella|daniel|polo' test.txt should output cindrella and daniel and not polo.

4

you want reverse greping.

printf "%s\n" cindrella daniel polo | grep -v -f test.txt
cindrella
daniel

where

  • -v is reverse option
  • -f test.txt take input list from test.txt
  • +1 To eliminate partial matches, the use of grep's -x flag would be worth considering, assuming both the input and the test.txt entries are one to a line. – Jim L. Apr 16 at 16:53
1

You can echo the name if grep doesn't match:

$ for i in cindrella daniel polo; do grep -q "$i" test.txt || echo "$i"; done
cindrella
daniel

-q quiet mode, exits after any match is found

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.