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.
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
# The g flag applies the replacement to all matches to the regexp, not just the first. #
sed 's/wiwek/vivek/g' names.txt
In the above example the “s” specifies the substitution operation. The “/” are delimiters. The mispelling “wiwek” is the search pattern (more like word in this case) 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 Linux, Open Source & DevOps via:
- RSS feed or Weekly email newsletter
- Share on Twitter • Facebook • 7 comments... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
$ echo Cool | sed -n "/cool/Ip"
sed version 4.1.5.
Thanks for the tip. This is exactly what I was looking for.
sed -n -e '/patternmatch/Ip' filetosearchin
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
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.
Hi,
Didn’t you miss a ‘g’ in the example at the beginning of the article?
Replacing or substituting string is simple. The below example replaces the word wiwek with vivek in the file:
sed 's/wiwek/vivek/' names.txt
For me it works only with sed 's/wiwek/vivek/g' names.txt
Miklos
Both syntax are valid, but the g flag applies the replacement to all matches to the regexp, not just the first one. Nevertheless, I added a small note.