Connect with us

Linux

Sed command in Linux with usage examples [2020]

Last Updated on

Let’s Learn Sed command in Linux with Practical Examples :

Short for stream editor, the sed command in Linux is a useful utility used for manipulation text output. You can use it for substituting text, finding and replacing text, searching text and so much more. The command accord you the ability to edit files without having to open them. In this tutorial, we look at various ways you can use the Sed command in Linux.

1) Replacing or substituting strings

To elaborate on how the command can be used for text manipulation, we are going to use a sample text file linuxgeek.txt with the content below:

To replace all instances of the word Unix with Linux, run the command:

Output

Linux sed command

The ‘s’ flag stands for substitution. In the command above, the first instance of the word ‘Unix’ is replaced by ‘Linux’. By default, the substitution happens to the first string occurrence only, and not the subsequent occurrences.

2) Replace the nth occurrence of a word in a line

To replace the nth occurrence of a string in a file, you need to use the /nth flag. For example, to replace the 2nd occurrence of a string in a line, issue the command:

Output

In the example above, the second instance of the string ‘Unix’ has been replaced by ‘Linux’ in the 1st and 4th lines.

3. Substitute all instances of a string in a file

If you want to replace all instances of the string to be substituted, in this case, ‘Unix’ use the /g option. The ‘g’ stands for global and instructs sed to replace all the instances of the string.

Output

4) Replace a string on a specific line number

Additionally, you can instruct the sed command in Linux to replace the keyword in a particular line. For example to replace the string in the 4th line only, use the command:

Output

Linux sed command

As observed, only the first instance is substituted. To substitute all the occurrences of the string, simple append ‘g’ after the last forward slash as shown:

Output

Linux sed command

5) Substitute a string in a range of lines

You can also opt to replace the search keyword for a range of lines, for example between lines 1 and 3. To accomplish this, issue the command:

Output

From the output above, we can observe that the substitution has only occurred in lines 1-3. The last line remains unchanged.

6) Print lines where substitution has occurred

To print the lines where substitution of the lines has occurred, use the -n flag immediately after the sed command as shown:

Output

7) Delete lines using Sed command in Linux

Sed command in Linux can also be used for deleting lines. This is possible using the ‘d’ flag. To delete the 3rd line, for example, run the command:

Output

To delete the last line only in the file, we are going to run the command:

8) Delete the last line

Output

9) Delete a range of lines

To delete a range of lines. for instance from 2 to 4 in a text file, run the command:

Output

10) Add blank lines or spaces

To add a blank line after every line run the command:

linux sed command

This wraps up our tutorial today. We have showcased some of the commonly used cases of the stream editor sed command.

Continue Reading
Click to comment

Leave a Reply

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

Trending