Let’s discuss some examples of SED command

Sed command or Stream Editor is one of the most utilised utility for Linux systems. It is mainly used for text substitution, find/replace operations & also other text manipulations like search, insertions deletion. We can also use regex or regular expressions with sed command, which further increases it’s usability. With sed command, we can perform all the operations on a file without opening it.

In this article, we will learn to use SED command by discussing some examples of Sed command. Syntax for using sed command is,

sed options… [script] [InputFile]

Recommended Read: Learn to use Wget command with 12 examples

Also Read: Beginner’s reference guide to NMAP command


Examples of Sed commands

Print partial text of a file

To print only some part of a file onto the screen rather than seeing whole file, use the following command,

$ sed -n 5,12p test.txt

Option ‘n’ used here will avoid printing of whole file, while option ‘p’ will print only line mentioned i.e. line 5 to 12.


Print all except some lines

To perform reverse of first example, i.e. to print complete contents of file except for some lines, use the following command,

$ sed 5,12d test.txt

Option ‘d’, here will remove the mentioned lines 5 to 12 , from printing on to the screen.


Deleting a line

To remove a line from a file, use the following command,

$ sed 5d test.txt

where ‘5’ is the line number, which can be that you want & option ‘d’ will delete the mentioned line number.


Deleting a number of lines

To remove a number of lines from the file, we can use the following command,

$ sed ‘5-12d’ test.txt

This will delete lines 5 to 12 from test.txt file.


Deleting lines other than the mentioned

To perform the reverse of above mentioned command i.e. to delete lines other than the lines that are mentioned, we can use the following command,

$ sed ‘5-12!d’ test.txt

here `!`, will reverse the command mentioned i.e. will not delete the lines mentioned & all other lines will be deleted from the file.


Display every 5th line starting with a mentioned line

Now let’s say that we want to print every 5th line of a file starting with line number 3. To do that, we can use the following command

$ sed -n ‘3-5p’ test.txt


Replacing a word/string

To search a word or a string & than replace it with another string , we will use the following example,

$ sed ‘s/dan/susan/’ test.txt

Option ‘s’ here will search for word ‘dan’ & than replace it with ‘susan’ for the first match it gets. Now if we need to replace the word completely from the file, we will use option ‘g’ along with ‘s’,

$ sed ‘s/dan/susan/g’ test.txt


Replace a particular occurrence of string/word

To replace a particular string of a string, for example 8th occurrence from a file, use the following command,

$ sed ‘s/dan/susan/8’ test.txt

here, 8th occurrence of the word will be replaced from the file. Now to replace a string starting from a particular occurrence, use

$ sed ‘s/dan/susan/8g’ test.txt

So here, 8th occurrence of the string will be replaced and all the matches after 8th string.


Replace a string on a particular line

To change a word from a particular line only, use the following examples of sed command as reference,

$ sed ‘9 s/dan/susan/’ test.txt

This will only substitute the string from 9th line of the file. We can also mention a range of lines instead of a single line,

$ sed ‘9-13 s/dan/susan/’ test.txt


Change a whole line with matched pattern

We can also change a complete line after a search pattern has been matched. To do this use the following examples of sed command,

$ sed ‘/dan c “Changes for the new line here” ’ test.txt

So when the pattern matches ‘dan’, whole line will be changed to the new line i.e. “Changes for the new line here” .


Add a line after/before the matched search

Now if we want to add a new line with some content & that too before a pattern match, than we can use option ‘i’ with sed command & pattern to be searched,

$ sed ‘/dan i “Adding text before match” ’ test.txt

To add a new line after every pattern match, the option that will be used with sed is option ‘a’ ,

$ sed ‘/dan a “adding text after match” ’ test.txt


Adding Blank lines/spaces

If we need to add a blank line after every non-blank line, we have to use option ‘G’,

$ sed G test.txt


Executing multiple sed commands

Now, if need to execute multiple sed expressions, we can do so using option ‘e’ to chain multiple sed commands . An example,

$ sed -e ‘s/dan/susan/g’ -e ‘s/hate/love/’ test.txt

Here we are searching replacing ‘dan’ with ‘susan’ & than also replacing ‘hate’ with ‘love’ from first occurrence.


Making a backup copy before editing a file

Sed command has power to alter complete files & that can be issue if something goes wrong, so it’s better to create a backup copy before editing with sed. To create a backup copy of a file before we edit it, use option ‘-i.bak’,

$ sed -i.bak -e ‘s/dan/susan/g’ test.txt

So the backup file will be named test.txt.bak.


Delete a file line starting with & ending with a pattern with regex

As mentioned above, we can also use regex with sed command to further enhance its capabilities. To delete a file line starting with a particular string & ending with another string, we can use following examples of sed command,

$ sed -e ‘s/dan.*stops//g’ test.txt

This will delete the line with ‘dan’ on start & ‘stops’ in the end & it can have any number of words in between , ‘.*’ defines that part.


Appending lines with regex

Another example for using sed with regex would be to add some content before every line. So use the following example,

$ sed -e ‘s/.*/added text &/’ test.txt

Every line will now start with ‘added text’.


Deleting all commented lines & empty lines

To only remove commented lines, use following example,

$ sed -e ‘s/#.*//’ test.txt

To remove all commented lines i.e. lines with # & all the empty lines, use following example

$ sed -e ‘s/#.*//;/^$/d’ test.txt

These were some examples of sed command. Please feel free to send in any queries or questions or suggestions you have using the comment box below.

If you have found this article to be useful, please share it among your friends/colleagues/followers. THANKS !!!

If you think we have helped you or just want to support us, please consider these :-

Connect to us: Facebook | Twitter | Google Plus

Donate us some of your hard earned money:

Linux TechLab is thankful for your continued support.


Passionate about Linux & open source. Loves to learn, read & write about Linux as well as new technologies.

You may also like...

3 Responses

  1. Ram says:

    What is /g here in commands

  2. Gangaram Yedage says:

    Thanks for sharing

Leave a Reply

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