Question

I am new to Linux world. I needed to discover the content called “foo” and changed to “bar” in the document named “abc.txt”. How would I utilize the sed command to find and change on Linux or UNIX-like framework?

Answer

The sed represents stream editor. It peruses the given document, changing the contribution as determined by a rundown of sed directions. Of course, the information is kept in touch with the screen, yet you can force to update document.

Find and change text inside a file utilizing sed command

The technique to change the content in files under Linux/Unix utilizing sed:

  1. Use Stream EDitor (sed) as mentioned below:
  2. sed -i 's/old-content/new-content/g' abc.txt
  3. The s is the substitute command of sed for find and change
  4. It advises sed to discover all events of ‘old-message‘ and change with ‘new-content‘ in a file named abc.txt
  5. Confirm that file has been updated:
  6. more abc.txt

Give us a chance to see linguistic structure and use in depth.

Syntax: sed find and supplant content

The syntax is:

sed 's/phrase1/phrase2/g' input.file
## *bsd/macos sed syntax#
sed 's/phrase1/phrase2/g' input.file > output.file
sed - I 's/phrase1/phrase2/g' input.file
sed - I - e 's/phrase1/phrase2/g' - e 's/xx/yy/g' input.file
## use + separator rather than/##
sed - I 's+regex+new-text+g' file.txt

The above supplant all events of characters in phrase1 in the example space with the comparing characters from phrase2.

Examples that utilizes sed to discover and change

Let’s create a file named hello.txt as following:

$ cat hello.txt
The is a test document made by HassanLatif.net for demonstartion purposes.
foo is great.
Foo is pleasant.
I love FOO.

I am going to utilize s/ for substitute the discovered expression foo with bar as following:

sed 's/foo/bar/g' hello.txt

The results are:

The is a test document made by HassanLatif.net for demonstartion purposes.
bar is great.
Foo is pleasant.
I love FOO.

To update the file pass the -i option:

sed -i 's/foo/bar/g' hello.txt
cat hello.txt

The g/implies global replace for example discover all events of foo and change with bar by utilizing sed. If you removed the /g the just first occurance is changed:

sed -i 's/foo/bar/' hello.txt

The / act as delimiter characters. To match the all instances of (foo, FOO, Foo, FoO) include I (capital I) as following:

sed -i 's/foo/bar/gI' hello.txt
cat hello.txt

The results are:

The is a test document made by HassanLatif.net for demonstartion purposes.
bar is great.
bar is pleasant.
I love bar.

If it’s not too much trouble note that the BSD execution of sed (FreeBSD/MacOS and co) does NOT support case-insensitive matching. You need to install gnu sed. Run the following command on Apple macOS:

$ brew install gnu-sed
######################################
### now use gsed command as follows ##
######################################
$ gsed -i 's/foo/bar/gI' hello.txt
$ cat hello.txt

sed command macOS

sed command issues

Take the following file as an example:

$ cat input.txt
http://is outdate.
Think about using https:// for all of your needs.

Discover word ‘http://’ and change with ‘https://hassanlatif.net’:

sed 's/http:///https://hassanlatif.net/g' input.txt

You will get an error that read as follows:

sed: 1: "s/http:///https://hassa ...": bad flag in substitute command: '/'

Our syntax is right yet the / delimiter character is also part of phrase1 and phrase2 in above exampl. Sed command enables you to change the delimiter / to something different. So I am going to utilize +:

sed 's+http://+https://hassanlatif.net+g' input.txt

The results are:

https://hassanlatif.net is outdate.
Think about utilizing https:// for all of your needs.

How to use sed to match phrase and perform find & change

In following example just find word ‘love’ and change it with ‘sick’ if line content a particular string, for example, FOO:

sed -i - e '/FOO/s/love/sick/' input.txt

Use cat command to confirm new changes:

cat input.txt

Recap: Using sed to find and change

The general syntax is:

## find word1 and replace with word2 using sed ##
sed -i 's/word1/word2/g' input
## you can change the delimiter to keep syntax simple ##
sed -i 's+word1+word2+g' input
sed -i 's_word1_word2_g' input
## you can add I option to GNU sed to case insensitive search ##
sed -i 's/word1/word2/gI' input
sed -i 's_word1_word2_gI' input

About the author

Hassan Latif

A Microsoft Certified Trainer, a hobby blogger and geek

Leave a Comment