[regex] 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.

This question is related to regex bash unix grep gnu

The answer is


If your grep -i does not work then try using tr command to convert the the output of your file to lower case and then pipe it into standard grep with whatever you are looking for. (it sounds complicated but the actual command which I have provided for you is not !).

Notice the tr command does not change the content of your original file, it just converts it just before it feeds it into grep.

1.here is how you can do this on a file

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

2.or in your case if you are just echoing something

echo "ABC"|tr '[:upper:]' '[:lower:]' | grep abc

It should 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

I'd suggest that the -i means it does match "ABC", but the difference is in the output. -i doesn't manipulate the input, so it won't change "ABC" to "abc" because you specified "abc" as the pattern. -o says it only shows the part of the output that matches the pattern specified, it doesn't say about matching input.

The output of echo "ABC" | grep -i abc is ABC, the -o shows output matching "abc" so nothing shows:

Naos:~ mattlacey$ echo "ABC" | grep -i abc | grep -o abc
Naos:~ mattlacey$ echo "ABC" | grep -i abc | grep -o ABC
ABC

Examples related to regex

Why my regexp for hyphenated words doesn't work? grep's at sign caught as whitespace Preg_match backtrack error regex match any single character (one character only) re.sub erroring with "Expected string or bytes-like object" Only numbers. Input number in React Visual Studio Code Search and Replace with Regular Expressions Strip / trim all strings of a dataframe return string with first match Regex How to capture multiple repeated groups?

Examples related to bash

Comparing a variable with a string python not working when redirecting from bash script Zipping a file in bash fails How do I prevent Conda from activating the base environment by default? Get first line of a shell command's output Fixing a systemd service 203/EXEC failure (no such file or directory) /bin/sh: apt-get: not found VSCode Change Default Terminal Run bash command on jenkins pipeline How to check if the docker engine and a docker container are running? How to switch Python versions in Terminal?

Examples related to unix

Docker CE on RHEL - Requires: container-selinux >= 2.9 What does `set -x` do? How to find files modified in last x minutes (find -mmin does not work as expected) sudo: npm: command not found How to sort a file in-place How to read a .properties file which contains keys that have a period character using Shell script gpg decryption fails with no secret key error Loop through a comma-separated shell variable Best way to find os name and version in Unix/Linux platform Resource u'tokenizers/punkt/english.pickle' not found

Examples related to grep

grep's at sign caught as whitespace cat, grep and cut - translated to python How to suppress binary file matching results in grep Linux find and grep command together Filtering JSON array using jQuery grep() Linux Script to check if process is running and act on the result grep without showing path/file:line How do you grep a file and get the next 5 lines How to grep, excluding some patterns? Fast way of finding lines in one file that are not in another?

Examples related to gnu

gcc-arm-linux-gnueabi command not found grep --ignore-case --only How to Export Private / Secret ASC Key to Decrypt GPG Files grep regex whitespace behavior Compiling a C++ program with gcc Passing additional variables from command line to make How to display line numbers in 'less' (GNU) How do I apply a diff patch on Windows? How do I syntax check a Bash script without running it? GnuPG: "decryption failed: secret key not available" error from gpg on Windows