Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grep --ignore-case --only

grep fails when using both --ignore-case and --only-match options. Example:

$ echo "abc" | grep -io abc abc $ echo "ABC" | grep -io abc $  

But

$ echo "abc" | grep -i abc abc $ echo "ABC" | grep -i abc ABC 

According to man page:

   -o, --only-matching           Show only the part of a matching line that matches PATTERN.    -i, --ignore-case           Ignore case distinctions in both the PATTERN and the input files. 

Is it a bug of grep or I didn't get the map page?

I am using Mac OS X 10.6.8 and

$ grep --version grep (GNU grep) 2.5.1 

Found this link: http://lists.gnu.org/archive/html/bug-gnu-utils/2003-11/msg00040.html

Of course it is possible to use workaround like grep -o [aA][bB][cC], but this doesn't seem to be a good option.

like image 862
schatten Avatar asked Dec 14 '11 01:12

schatten


People also ask

How do I grep in Ignore case?

Grep Case Insensitive with -i Option The case insensitive search can be made with the -i option for the grep command.

Does grep match case?

Grep is case-sensitive by default hence it shows the perceptibility of both upper and lower cases in the file.

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”.


2 Answers

This is a known bug on the initial 2.5.1, and has been fixed in early 2007 (Redhat 2.5.1-5) according to the bug reports. Unfortunately Apple is still using 2.5.1 even on Mac OS X 10.7.2.

You could get a newer version via Homebrew (3.0) or MacPorts (2.26) or fink (3.0-1).


Edit: Apparently it has been fixed on OS X 10.11 (or maybe earlier), even though the grep version reported is still 2.5.1.

like image 78
kennytm Avatar answered Sep 21 '22 08:09

kennytm


It could be a problem in your version of grep.

Your test cases are working correctly here on my machine:

$ echo "abc" | grep -io abc abc $ echo "ABC" | grep -io abc ABC 

And my version is:

$ grep --version grep (GNU grep) 2.10 
like image 25
Felix Yan Avatar answered Sep 21 '22 08:09

Felix Yan