Sed Command in unix

Sed (Stands for stream editor) is a command line utility to modify each line of a file or stream matching a specified string. It makes basic text changes to a file or input from a pipeline.

Sed command can perform variety of functions on file like, searching, find and replace, insertion or deletion. Most common use of Sed command is for substitution or to find and replace. With SED command, you can edit files even without opening them, which is much quicker way to find and replace something in file, than first opening that file in Editor and then changing it.

An example can help understanding the Sed command. We have a file named sed_demo.txt having these lines.

$ cat sed_demo.txt >
1. Nike Shoes – Tag 344 – Price $ 16
2. Rebook shoes – Tag 433 – Price $ 17
3. Bata Shoes – Tag 211 – Price $ 18
4. Summer Shoes – Tag 978 – Price $ 16

We want to change the price of $ 16 with $ 19. Sed command allows us to do that

$ sed ‘s/16/19/’ sed_demo.txt

On execution of above command you will see something like this

$ cat sed_demo.txt
1. Nike Shoes – Tag 344 – Price $ 19
2. Rebook shoes – Tag 433 – Price $ 17
3. Bata Shoes – Tag 211 – Price $ 18
4. Summer Shoes – Tag 978 – Price $ 19

If we want to change in file, then use below command, use below command.

$ sed -i -e ‘s/16/19/’ sed_demo.txt

Price of Nike Shoes and Summer Shoes are now increased to $ 19 (shown in bold).

Here is another way of doing this

$ sed ‘s/16/19/’ sed_demo.txt > sed_demo_new.txt

this command will also change price of Nike Shoes and Summer Shoes to 19 but changes are saved it in a new file with name sed_demo_new.txt, in this case original file sed_demo.txt remains unaffected.

With sed command you can extract lines having specific string.

$ sed -n ‘/Bata/p’ sed_demo.txt > sed_demo_new.txt

this will extract all lines having ‘Bata’ and paste it into a new file named sed_demo_new.txt.

You can apply sed command to STDIN also like this,

$ echo ‘Welcome to My Linux Blog’ | sed ‘s/Blog/website/’

The s option replaces the first text with the second text pattern. In this case, the string "Blog" will be replaced with "website". Output of this command will be

Welcome to My Linux website

Sed command has /1, /2 etc flags that can replace the first, second occurrence of a pattern in a line. The below command replaces the second occurrence of the word "shoes" with "shirt" in a line.

$ sed ‘s/shoes/Shirt/2’ sed_demo.txt

Sed command has substitute flag /g (global replacement) that replaces all the occurrences of the string in the line.

$ sed ‘s/shoes/Shirt/g’ sed_demo.txt

This will replace every occurrence of shoes with shirt in sed_demo.txt file.

You can also combine /1, /2 etc with /g to replace all the patterns from the nth occurrence of a pattern in a line. The following sed command replaces the second onward occurrence of "shoes" word with "Shirt" word in a line.

$ sed ‘s/shoes/Shirt/2g’ sed_demo.txt

To learn more on Sed command type $ man Sed on your unix distribution.

That’s all about Sed command in unix.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *