sed Case Insensitive Search Matching and Replacement

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/. Let us see how to use sed for case insensitive search and replace under Linux or Unix-like systems including macOS.
sed Case Insensitive Search

ADVERTISEMENTS

UNIX / Linux: sed Case Insensitive Search and Replace

Replacing or substituting string is simple. The below example replaces the word wiwek with vivek in the file:
sed 's/wiwek/vivek/' names.txt
In the above example the “s” specifies the substitution operation. The “/” are delimiters. The mispelling “wiwek” is the search pattern and the “vivek” is the replacement string. By default, sed is case-sensitive. You can add an “i” OR “I” flag at the end of the substitution to change this. 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
sed -i 's/Unix/Linux/gi' input

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

A note about GNU sed version

The syntax is pretty simple
sed -i 's/SEARCH-WORD/REPLACMENT-WORD/gi' input
The -i option edit and update file in place.

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

Please note that macOS comes with BSD version of sed which does not support case-insensitive matching. Hence, 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

Onec gsed installed, create the alias as follows:
alias sed='gsed'
Now we can use the sed command as follows:
sed -i 's/Unix/Linux/gI' file

A note about Perl for case-insensitive search and replace instead of sed

Another option is to use the perl tool for case-insensitive search & replace as follows:

## find foo and replace with bar case-insensitive ##
perl -pi -e 's/old_word/new_word/gi' file
perl -pi -e 's/SEARCH/REPLACE/gi' filename.txt
perl -pi -e 's/foo/bar/gi' input.txt

Use the cat command to verify changes:
cat input.txt
We can replace a single word in a large number of files in Unix or Linux using the Perl as follows:
perl -pi -e 's/cyberciti/nixcraft/gi' *.txt
Perl would change every instance of “cyberciti” it found into “nixcraft.

Conclusion

You learned about case-insensitive search and replace with sed under Linux, macOS and Unix-like systems. See sed command man page here for more info or type the following command at the shell promot:
man sed

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
5 comments… add one
  • Ni2 Dec 21, 2009 @ 7:48

    $ echo Cool | sed -n “/cool/Ip”

  • Ni2 Dec 21, 2009 @ 7:53

    sed version 4.1.5.

    Thanks for the tip. This is exactly what I was looking for.

  • skymaster Sep 26, 2013 @ 10:14

    sed -n -e ‘/patternmatch/Ip’ filetosearchin

  • arun Jan 1, 2017 @ 12:39

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

    [root@localhost ~]# 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

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

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

  • Jonas Steinberg Dec 15, 2017 @ 2:19

    You don’t need to cat-pipe the file to sed as sed natively takes a file as its second positional parameter, i.e. sed -i s’/replace/this/gI’ my_file.txt.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.