By john


2010-12-27 11:09:06 8 Comments

I need something like:

grep ^"unwanted_word"XXXXXXXX

8 comments

@codaddict 2010-12-27 11:21:55

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

@Andrey Regentov 2014-11-06 05:01:33

what if I want to exclude N lines after the line with "unwanted word" as well? -v 'unwanted_word' --after N doesn't help because it INCLUDES the line and N lines after.

@adamski.pro 2016-11-29 07:10:07

-v or --invert-match select non-matching lines. In your case grep -v 'unwanted_word' file or grep --invert-match 'unwanted_word' file.

@Kanji Viroja 2016-12-20 10:38:50

I want to ignore one line above and one line below with matching pattern then How can i achieve it?

@benjaminz 2017-01-06 16:12:47

Awesome, I use this in git to quickly peruse the status of my repo, works like a charm: git status -s |grep -v "folder_I_dont_care"

@greene 2018-03-24 10:05:42

Weird, it's the top answer, but in some cases it's wrong! If I want to find sun, except when it is sunrise, grep sun|grep -v sunrise skips line that contain both sun and sunrise at once, that is not what I want. grep -P 'sun(?!rise)' is much better.

@dwanderson 2018-06-18 19:05:16

@greene provided the key - excluding the term in the same query. I'm using ag, not grep per se, and I can't pipe one ag to another. What I needed was (?!foo) - thanks!

@allenyllee 2018-09-28 04:03:59

If you want to bring the regex power into the exclude pattern, just add -E. e.g. grep -v -E "unwanted_pattern_in_regex" file

@pjrieger 2015-10-08 19:29:11

I've a directory with a bunch of files. I want to find all the files that DO NOT contain the string "speedup" so I successfully used the following command:

grep -iL speedup *

@xuiqzy 2016-06-07 22:45:11

From the man page: "-L, --files-without-match Suppress normal output; instead print the name of each input file from which no output would normally have been printed. The scanning will stop on the first match." (Emphasis by me) So beware of this!

@fedorqui 'SO stop harming' 2015-04-09 21:11:35

The right solution is to use grep -v "word" file, with its awk equivalent:

awk '!/word/' file

However, if you happen to have a more complex situation in which you want, say, XXX to appear and YYY not to appear, then awk comes handy instead of piping several greps:

awk '/XXX/ && !/YYY/' file
#    ^^^^^    ^^^^^^
# I want it      |
#            I don't want it

You can even say something more complex. For example: I want those lines containing either XXX or YYY, but not ZZZ:

awk '(/XXX/ || /YYY/) && !/ZZZ/' file

etc.

@MBR 2016-05-13 15:15:09

It appears to be much faster than the grep -P solution on big files.

@fedorqui 'SO stop harming' 2016-05-14 22:28:10

@MBR grep -P means using Perl regexp, so loading that package is going to be way more expensive than a normal grep.

@Shriganesh Shintre 2015-03-27 19:01:59

grep provides '-v' or '--invert-match' option to select non-matching lines.

e.g.

grep -v 'unwanted_pattern' file_name

This will output all the lines from file file_name, which does not have 'unwanted_pattern'.

If you are searching the pattern in multiple files inside a folder, you can use the recursive search option as follows

grep -r 'wanted_pattern' * | grep -v 'unwanted_pattern'

Here grep will try to list all the occurrences of 'wanted_pattern' in all the files from within currently directory and pass it to second grep to filter out the 'unwanted_pattern'. '|' - pipe will tell shell to connect the standard output of left program (grep -r 'wanted_pattern' *) to standard input of right program (grep -v 'unwanted_pattern').

@codaddict 2010-12-27 11:14:16

If your grep supports Perl regular expression with -P option you can do (if bash; if tcsh you'll need to escape the !):

grep -P '(?!.*unwanted_word)keyword' file

Demo:

$ cat file
foo1
foo2
foo3
foo4
bar
baz

Let us now list all foo except foo3

$ grep -P '(?!.*foo3)foo' file
foo1
foo2
foo4
$ 

@DocWiki 2011-07-16 17:49:24

Thanks for this, very useful! I would like to mention that The grep command is case sensitive by default

@cybersoft 2015-10-29 09:23:12

Note that grep -v -P also works without negation in regular expression.

@Gabriel Staples 2020-04-19 06:55:03

"if bash...you'll need to escape the !". Thank you thank you thank you! That's what I wanted!

@JPGConnolly 2012-11-21 08:07:56

I understood the question as "How do I match a word but exclude another", for which one solution is two greps in series: First grep finding the wanted "word1", second grep excluding "word2":

grep "word1" | grep -v "word2"

In my case: I need to differentiate between "plot" and "#plot" which grep's "word" option won't do ("#" not being a alphanumerical).

Hope this helps.

@Matthew Read 2015-06-16 20:45:02

You should reverse the order to get highlighting on word1.

@patrick 2018-08-11 21:57:48

I guess it would clarify to add a placeholder for the file name to that example

@garima 2010-12-27 11:16:42

Invert match using grep -v:

grep -v "unwanted word" file pattern

@st0le 2010-12-27 11:16:18

The -v option will show you all the lines that don't match the pattern.

grep -v ^unwanted_word

Related Questions

Sponsored Content

67 Answered Questions

[SOLVED] How to replace all occurrences of a string?

13 Answered Questions

[SOLVED] grep a file, but show several surrounding lines?

49 Answered Questions

[SOLVED] How do I find all files containing specific text on Linux?

31 Answered Questions

[SOLVED] Regular expression to match a line that doesn't contain a word

79 Answered Questions

[SOLVED] How do I iterate over the words of a string?

  • 2008-10-25 08:58:21
  • Ashwin Nanjappa
  • 2183667 View
  • 2999 Score
  • 79 Answer
  • Tags:   c++ string split

3 Answered Questions

22 Answered Questions

[SOLVED] Use grep --exclude/--include syntax to not grep through certain files

15 Answered Questions

[SOLVED] How to grep (search) committed code in the Git history

  • 2010-05-28 11:36:06
  • Ortwin Gentz
  • 399425 View
  • 1447 Score
  • 15 Answer
  • Tags:   git grep diff

24 Answered Questions

[SOLVED] How do I grep recursively?

  • 2010-01-01 05:10:12
  • wpiri
  • 1552765 View
  • 1693 Score
  • 24 Answer
  • Tags:   linux unix grep

14 Answered Questions

[SOLVED] Can grep show only words that match search pattern?

  • 2009-10-10 00:47:25
  • Neil Baldwin
  • 710114 View
  • 699 Score
  • 14 Answer
  • Tags:   grep words

Sponsored Content