Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I exclude one word with grep?

I need something like:

grep ^"unwanted_word"XXXXXXXX 
like image 839
john Avatar asked Dec 27 '10 11:12

john


People also ask

How do I filter out grep?

grep is very often used as a "filter" with other commands. It allows you to filter out useless information from the output of commands. To use grep as a filter, you must pipe the output of the command through grep . The symbol for pipe is " | ".

How do I exclude a character in grep?

Excluding words To exclude particular words or lines, use the –invert-match option. Use grep -v as a shorter alternative.

How do you grep everything except?

How to Exclude a Single Word with grep. The most simple way to exclude lines with a string or syntax match is by using grep and the -v flag. The output will be the example. txt text file but excluding any line that contains a string match with “ThisWord”.


1 Answers

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 
like image 115
codaddict Avatar answered Nov 12 '22 02:11

codaddict