0

I am using grep -v "cat" input.txt > output.txt to remove all lines in input.txt containing "cat" and outputing the result in output.txt. How can I remove all lines containing "cat" and/or "dog" and/or "fish" and/or "sheep"? This works, but requires many lines:

grep -v "cat" input.txt > output.txt
mv output.txt input.txt
grep -v "dog" input.txt > output.txt
mv output.txt input.txt
grep -v "fish" input.txt > output.txt
mv output.txt input.txt
grep -v "sheep" input.txt > output.txt
mv output.txt input.txt

I tried this `grep -v [cat|dog|fish|sheep] input.txt > output.txt, but that did not work.

How can I remove any line containing at least one of these items?

Village
  • 22,513
  • 46
  • 122
  • 163
  • Your attempt is almost good: just use `egrep` instead, or escape each one of the pipes: `grep -v "cat\|dog..."`. – fedorqui Aug 17 '14 at 13:08

1 Answers1

1

You should escape the pipes:

grep -v "cat\|dog\|fish\|sheep" input.txt > output.txt

Otherwise you can can tell grep to accept a perl-style regex with the option -P <regex>:

grep -v -P "(cat|dog|fish|sheep)" input.txt > output.txt
enrico.bacis
  • 30,497
  • 10
  • 86
  • 115