sed Case Insensitive Search Matching

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

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

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

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 https://homebrew.bintray.com/bottles/gnu-sed-4.4.sierra.bottle.tar.gz
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:
  PATH="/usr/local/opt/gnu-sed/libexec/gnubin:$PATH"

Additionally, you can access their man pages with normal names if you add
the "gnuman" directory to your MANPATH from your bashrc as well:
  MANPATH="/usr/local/opt/gnu-sed/libexec/gnuman:$MANPATH"
==> 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

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

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

Posted by: SXI ADMIN

The author is the creator of SXI LLC and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.