Connect with us

Hi, what are you looking for?

Latest

Sed Case Sensitive Search Matching and Replacement

Sed Case Sensitive Search Matching and Replacement

Sed Case Sensitive Search Matching and Replacement

How do I perform a case-insensitive search with sed under UNIX or Linux operating systems? I want to compare all word combinations – foo, FOO, FoO and the like – when they are replaced or other operations are performed with the sed command.

GNU sed and other versions support case insensitivity searching with the I flag for /regex/. Let’s see how we can use sed for case-insensitive searches and replace it with Linux or Unix-like systems, including MacOS.
Sed Case Sensitive Search Matching and Replacement

 

UNIX / Linux: case insensitive sed Search and replace

It is very easy to replace a line. In the following example, the word wiwek in the file is replaced by vivek:
sed ‘s/wiwek/vivek/’ names.txt
In the example above, s defines a replacement operation. / are separators. The error wwek is a search pattern, and vivek is a replacement string. By default, Sorry is case sensitive. You can add an i or I indicator at the end of the replacement to change it. To perform a case-specific 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.

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’re using an older version of sed, give it a try,

sed ‘s/[wW][oO][rR][dD]/substitute word /g’ input.txt > output.txt

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

It is easy to combine the first characters, for example to associate both Linux and the word linux:

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

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

GNU sed versionNote

Simple syntax
sed -i’s/SEARCH-WORD/REPLACMENT-WORD/gi’ input
Option -i edits and updates the file on the spot.

The BSD implementation in sed mode is NOT case sensitive tomapping.

Note that macOS comes with a BSD version of sed that does not support case-sensitive modification. So if you are on MacOS, install GNU sed with the following command to brew:
$ brow set gnu-sed
Trial Outputs :

The development of a…
==> Homebrew updated automatically!
Update 1 tap (home/core).
No changes to the formulas.

==> Download 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
==> Casting gnu-sed-4.4.sierra.bottle.tar.gz
==> Using the sandbox
==> Reservations
The command was preceded by g.
If you do not need a prefix, set it with the Default names option.

If you need to use these commands with their common name, you can add
the gnubin directory to your bashrc’s PATH as :
PATH=/usr/local/opt/gnu-sed/libexec/gnubin:$PATH

In addition, you can access their man pages with normal names if you add the gnuman
directory from bashrc to your MANPATH:
MANPATH=/usr/local/opt/gnu-sed/libexec/gnuman:$MANPATH
==> Abstract
? /usr/local/Cellar/gnu-sed/4.4: 12 folders, 491KB

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 the 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

Onec gsed is installed, create an alias as follows:
alias sed=’gsed’
We can now use the command sed as follows:
sed -i ‘s/Unix/Linux/gI’ file

Note for Perl for search and replacement by sed

Another possibility is to use the Perl tool for case-insensitive searches and replace it as follows:

## find foo and replace it with a case sensitive bar ##
perl -pi -e ‘s/old_word/new_word/gi’ file
perl -pi -e ‘s/SEARCH/REPLACE/gi’ file name.txt
perl -pi -e ‘s/foo/bar/gi’ input.txt

## 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 check for changes:
cat input.txt
We can replace a word in many Unix or Linux files with Perl :
perl -pi -e ‘s/cyberciti/nixcraft/gi’ *.txt
Perl will change any instance of Cyberciti it finds in nixcraft.

Conclusion

They learned case-insensitive searching and sed-substitution on Linux, MacOS and Unix-like systems. More information can be found here on the sed command line page or type the following command in the shell promoter:
man sed

RECOMMENDATIONS

Published: Vivek gears

The author is the creator of nixCraft and an experienced system administrator, DevOps engineer and Linux/Unix shell script trainer. Receive the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly by email.sed case insensitive delete,sed replace case-insensitive,sed find and replace,awk case insensitive,sed print case insensitive,grep case insensitive,sed replace keep case,regex case insensitive

You May Also Like

Hosting

The latest round of MITRE ATT&CK evaluations proved yet again that Microsoft customers can trust they are fully protected even in the face of...

Hosting

On Thursday, April 23rd, 2020, Canonical Ltd, the makers of Ubuntu Linux distribution officially released the long-awaited Ubuntu 20.04 version code-named “Focal Fossa”, it...

Hosting

As the robot process automation (RPA) market becomes more and more dynamic, more and more companies are trying to integrate RPA into their business...

Latest

Virtual Machine Manager is one of the best hypervisors available for the Linux desktop. This is well-designed and well-functioning QEMU/KVM virtualization software that takes...