How can I exclude one word with grep?

john picture john · Dec 27, 2010 · Viewed 785.9k times · Source

I need something like:

grep ^"unwanted_word"XXXXXXXX

Answer

codaddict picture codaddict · Dec 27, 2010

You can do it using -v (for --invert-match) option of grep as:

grep -v "unwanted_word" file | grep XXXXXXXX

grep -v "unwanted_word" file will filter the lines that have the unwanted_word and grep XXXXXXXX will list only lines with pattern XXXXXXXX.

EDIT:

From your comment it looks like you want to list all lines without the unwanted_word. In that case all you need is:

grep -v 'unwanted_word' file