Delete line that contains a case insensitive match

awkcase sensitivitygrepsed

I have a file that contains information as so:

20    BaDDOg
31    baddog
42    badCAT
43    goodDoG
44    GOODcAT

and I want to delete all lines that contain the word dog. This is my desired output:

42    badCAT
44    GOODcAT

However, the case of dog is insensitive.

I thought I could use a sed command: sed -e "/dog/id" file.txt , but I can't seem to get this to work. Does it have something to do with me working on an OSX? Is there any other method I could use?

Best Answer

Try grep:

grep -iv dog inputfile

-i to ignore case and -v to invert the matches.

If you want to use sed you can do:

sed '/[dD][oO][gG]/d' inputfile

GNU sed extends pattern matching with the I modifier, which should make the match case insensitive but this does not work in all flavors of sed. For me, this works:

sed '/dog/Id' inputfile

but it won't work on OS X.

Related Question