sed Case Insensitive Search Matching

Posted on in Categories , , , , , , , , last updated May 24, 2017

How do I perform a case-insensitive search using sed under UNIX-like or Linux operating system? I would like to match all combination of word – foo, FOO, FoO and so on while replacing or performing other operations using sed command.

GNU sed and other version does support a case-insensitive search using I flag after /regex/.

UNIX / Linux: sed Case Insensitive Search and Replace

To perform a case-insensitive search, enter:

cat file.txt | sed -e 's/find-word/replace-word/gI' 
cat file.txt | sed -e 's/find-word/replace-word/gI' > output.txt
sed 's/find-word/replace-word/gI' input.txt > output.txt

If you are using older sed version try,

sed 's/[wW][oO][rR][dD]/replace-word/g' input.txt > output.txt

It is easy to match first few characters, for example match both Linux and linux word:

sed 's/[Ll]inux/Unix/g' input.txt > output.txt

The BSD implementation of sed does NOT support case-insensitive matching

If you are on macOS install GNU sed using the following brew command:
$ brew install gnu-sed
Sample outputs:

Updating Homebrew...
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/core).
No changes to formulae.

==> Downloading
Already downloaded: /Users/veryv/Library/Caches/Homebrew/gnu-sed-4.4.sierra.bottle.tar.gz
==> Pouring gnu-sed-4.4.sierra.bottle.tar.gz
==> Using the sandbox
==> Caveats
The command has been installed with the prefix "g".
If you do not want the prefix, install using the "with-default-names" option.

If you need to use these commands with their normal names, you
can add a "gnubin" directory to your PATH from your bashrc like:

Additionally, you can access their man pages with normal names if you add
the "gnuman" directory to your MANPATH from your bashrc as well:
==> Summary
?  /usr/local/Cellar/gnu-sed/4.4: 12 files, 491KB

Now use gsed command as follows:

cat file.txt | gsed -e 's/find-word/replace-word/gI' 
cat file.txt | gsed -e 's/find-word/replace-word/gI' > output.txt
gsed 's/find-word/replace-word/gI' input.txt > output.txt

Another option is to use perl command for case-insensitive search & replace:

## find foo and replace with bar case-insensitive ##
perl -pi -e 's/foo/bar/gi' input.txt

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

4 comment

  1. Hi,
    Printing with case sensitive is not working, as suggested by you. Could you check once again.

    [[email protected] ~]# yum install sed
    Loaded plugins: refresh-packagekit, rhnplugin
    Setting up Install Process
    Package sed-4.2.1-5.el6.x86_64 already installed and latest version
    Nothing to do

    [[email protected] ~]# sed -n ‘/oracle/’p passwd
    40 oracle:x:508:510::/home/oracle:/bin/bash
    41 Oracle:x:508:510::/home/oracle:/bin/bash

    [[email protected] ~]# sed -n ‘/oracle/’Ip passwd
    40 oracle:x:508:510::/home/oracle:/bin/bash
    41 Oracle:x:508:510::/home/oracle:/bin/bash

Leave a Comment