Grep --ignore-case - only

grep fails when using the --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 the 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 this a grep error or didn’t I get the map page?

I am using Mac OS X 10.6.8 and

 $ grep --version grep (GNU grep) 2.5.1 

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

Of course, you can use a workaround, for example grep -o [aA][bB][cC] , but this does not seem to be a good option.

+51
unix bash regex grep gnu
Dec 14 2018-11-11T00:
source share
4 answers

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

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




Edit: Apparently, it was fixed in OS X 10.11 (or maybe earlier), although the grep version is still 2.5.1.

+38
Dec 14 '11 at 1:17
source share

This should be a problem in your version of grep.

Your test cases work correctly on my machine:

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

And my version:

 $ grep --version grep (GNU grep) 2.10 
+5
Dec 14 '11 at 1:10
source share

If your grep -i does not work, try using the tr command to convert the output of your file to lowercase and then pass it to standard grep with what you are looking for. (it sounds complicated, but the actual command that I have provided to you is not!).

Note that the tr command does not modify the contents of the source file, but simply converts it immediately before sending it to grep.

1. Here is how you can do it in a file

 tr '[:upper:]' '[:lower:]' <your_file.txt|grep what_ever_you_are_searching_in_lower_case 

2.or in your case, if you just echo something

 echo "ABC"|tr '[:upper:]' '[:lower:]' | grep abc 
+2
Jan 09 '13 at 18:23
source share

I would suggest that -i means it matches "ABC", but the difference is in output. -i does not control the input, so it will not change "ABC" to "abc" because you specified "abc" as a template. -o says that it shows only the part of the result that matches the specified pattern, it does not indicate an input match.

Output echo "ABC" | grep -i abc echo "ABC" | grep -i abc is equal to ABC , -o shows the correspondence of the output "abc", so nothing is displayed:

 Naos:~ mattlacey$ echo "ABC" | grep -i abc | grep -o abc Naos:~ mattlacey$ echo "ABC" | grep -i abc | grep -o ABC ABC 
0
Dec 14 '11 at 1:11
source share



All Articles