Sed Command in Linux with Examples

SED abbreviated as Stream Editor is a powerful text stream editor. It can perform a lot of functions on file that is searching, finding and replacing, insertion and deletion. Though, SED is primarily or mainly used for text substitution.

With the help of SED, we can edit files without opening it, which will be much faster and quicker to find and replace something in a file, rather than opening the file and changing it.

Wanted to learn some basic Linux commands?

Read More: Frequently Used Basic Linux Commands

In this article, we’ll learn to use SED Commands with some examples that will be explained in detail.

Basic Syntax of SED Command:

sed OPTIONS... [SCRIPT] [INPUTFILE...] 

Example:

Consider the below text as an input.

$cat > sedcommands.txt
sed command is known as stream editor. Learn on How to use sed command in linux.
sed command is used to perform operations on files. sed is used for replacing strings.
What is sed in linux. sed also has many flags.
sed has several commands.

SED Command in Linux

1. Replacing String

SED command is mostly used to replace the text in a file. This command replaces the word “sed” with “SED” in the file.

$sed 's/sed/SED/' sedcommands.txt

Output:

SED command is known as stream editor. Learn on How to use sed command in linux.
SEDcommand is used to perform operations on files. sed is used for replacing strings.
What is SED in linux. sed also has many flags.
SED has several commands.

Here, “s” specifies the substitution operation. The “/” are delimiters (characters that separate text strings). “sed” is the search pattern and the “SED” is the replacement string.

By default, the sed command replaces the only first occurrence of the pattern in each line. It won’t replace the second, third and other occurrences in the line.

2. Replacing the nth Occurrence of a Pattern

In this command, /1, /2, etc. flags are used to replace the first, second occurrence of a pattern in a line. This command replaces the second occurrence of the word “sed” with “SED” in a line.

$sed 's/sed/SED/2' sedcommands.txt

Output:

sed command is known as stream editor. Learn on How to use SED command in linux.
sed command is used to perform operations on files. SED is used for replacing strings.
What is sed in linux. SED also has many flags.
sed has several commands.

3. Replacing All the Occurrence of the Pattern

In this command, /g flag is used to replace all the occurrences of the string in the line.

$sed 's/sed/SED/g' sedcommands.txt

Output:

SED command is known as stream editor. Learn on How to use SED command in linux.
SED command is used to perform operations on files. SED is used for replacing strings.
What is SED in linux. SED also has many flags.
SED has several commands.

4. Replacing from nth Occurrence to All the Occurrences

In this command, we can use the combination of /1, /2, etc and /g to replace all the patterns from the nth occurrence of a pattern in a line. Here, the command replaces second, third … “sed” word with “SED” word in a line.

$sed 's/sed/SED/2g' sedcommands.txt

Output:

sed command is known as stream editor. Learn on How to use SED command in linux.
sed command is used to perform operations on files. SED is used for replacing strings.
What is sed in linux. SED also has many flags.
sed has several commands.

5. Parenthesizing the First Character of Each Word

In this sed command, the first character of every word is in parenthesis.

$ echo "Sed Also Known As Stream Editor" | sed 's/\(\b[A-Z]\)/\(\1\)/g'

Output:

(S)ed (A)lso (K)nown (A)s (S)tream (E)ditor

6. Replacing String on a Specific Line Number

In this sed command, we can restrict the function only to replace the string on a specific line number.

$sed '2 s/sed/SED/' sedcommands.txt

Output:

sed command is known as stream editor. Learn on How to use sed command in linux.
SED command is used to perform operations on files. SED is used for replacing strings.
What is sed in linux. sed also has many flags.
sed has several commands.

In the above sed command, we can find that only the second line is replaced by “SED”.

7. Duplicating the Replaced Line with /p Flag

In this sed command, the /p flag prints the replaced line twice. If the line does not have the search pattern then it is not replaced and prints that line only once.

$sed 's/sed/SED/p' sedcommands.txt

Output:

SED command is known as stream editor. Learn on How to use SED command in linux.
SED command is known as stream editor. Learn on How to use SED command in linux.
SED command is used to perform operations on files. SED is used for replacing strings.
SED command is used to perform operations on files. SED is used for replacing strings.
What is SED in linux. SED also has many flags.
What is SED in linux. SED also has many flags.
SED has several commands.
SED has several commands.

As you can see here, the last line doesn’t have the search pattern. So, it is printed once only but other lines have the search pattern so they are displayed twice.

8. Printing only the Replaced Lines

In this sed command, -n removes the duplicated rows generated by the /p flag and prints the replaced lines only one time.

$sed -n 's/sed/SED/p' sedcommands.txt

Output:

SED command is known as stream editor. Learn on How to use SED command in linux.
SED command is used to perform operations on files. SED is used for replacing strings.
What is SED in linux. SED also has many flags.
SED has several commands.

9. Displaying Partial Text

In this sed command, we can view only some part of a file rather than the whole file.

$sed -n 2,3p sedcommands.txt

Output:

sed command is used to perform operations on files. sed is used for replacing strings.
What is sed in linux. sed also has many flags.

As you can see, only the second and third lines are printed. And, the first and the last line is not printed.

10. Displaying All Except Some Lines

Using this sed command, we can view the content of the file except for some portion. ‘d’ will remove the lines that are mentioned in the command like second and third from the output.

$sed 2,3d sedcommands.txt

Output:

sed command is known as stream editor. Learn on How to use sed command in linux.
sed has several commands.

11. Deleting a Line

Using this sed command, we can delete a line from the file.

$sed Nd sedcommands.txt

where ‘N’ can be any line number that you want to delete. ‘d’ will delete the mentioned line number.

$sed 2d sedcommands.txt

Output:

sed command is known as stream editor. Learn on How to use sed command in linux.
What is sed in linux. sed also has many flags.
sed has several commands.

Here, the second line is deleted from the file.

12. Deleting a Range of Lines from a file

We can use this sed command in order to delete a range of lines from the files.

$sed '2,4d' sedcommands.txt

Output:

sed command is known as stream editor. Learn on How to use sed command in linux.

Here, only the first line is displayed because second, third and fourth lines are deleted from the sedcommands.txt file.

13. Deleting the Pattern Matching Line

We can use this sed command to delete the line that has the pattern we mentioned.

$sed '/several/d' sedcommands.txt

Output:

sed command is known as stream editor. Learn on How to use sed command in linux.
sed command is used to perform operations on files. sed is used for replacing strings.
What is sed in linux. sed also has many flags.

14. Running Multiple SED Commands

If we have to perform multiple sed expressions then we need to use option ‘e’ to chain the sed commands.

$sed -e 's/sed/SED/g' -e '/several/d' sedcommands.txt 

Output:

SED command is known as stream editor. Learn on How to use SED command in linux.
SED command is used to perform operations on files. SED is used for replacing strings.
What is SED in linux. SED also has many flags.

15. Creating a Backup Copy before Editing a file

To create a backup copy of a file before we edit it, use option ‘-i.bak’.

$sed -i.bak -e 's/sed/SED/g'  sedcommands.txt

This will create a backup copy of the file with extension .bak. You can also use other extensions if you like.

16. Deleting a line Starting with & Ending with a Pattern

Using this sed command, you can delete a line starting with a particular string & ending with another string.

$sed -e 's/^sed.*linux$//g' sedcommands.txt

Output:

SED command is used to perform operations on files. SED is used for replacing strings.
What is SED in linux. SED also has many flags.
SED has several commands.

In the above output, the line with starting ‘sed’ and ending with ‘linux’ is deleted.

17. Appending lines

This sed command is used to add some content before every line using regex.

$sed -e 's/.*/Thr &/' sedcommands.txt

Output:

The sed command is known as stream editor. Learn on How to use sed command in linux.
The sed command is used to perform operations on files. sed is used for replacing strings.
The What is sed in linux. sed also has many flags.
The sed has several commands.

In the above output, we can see ‘The’ is added in front of every line.

18. Getting a list of all Usernames from /etc/passwd

This sed command is used to get all the list of all usernames from /etc/passwd file.

$sed 's/\([^:]*\).*/\1/' /etc/passwd

The above command will generate all the usernames available of the machine as an output.

19. Removing all Commented lines & Empty lines

With the help of this sed command, we can remove all commented lines with # and all the empty lines.

$ sed -e 's/#.*//;/^$/d' sedcommands.txt

To remove only commented lines:

$ sed -e 's/#.*//' sedcommands.txt

20. Inserting a Blank line after Each line

This command will help to insert each blank line after each line of a file.

$sed G sedcommands.txt

Output:

sed command is known as stream editor. Learn on How to use sed command in linux.

sed command is used to perform operations on files. sed is used for replacing strings.

What is sed in linux. sed also has many flags.

sed has several commands.

To insert two blank lines:

$sed 'G;G' sedcommands.txt

Output:

sed command is known as stream editor. Learn on How to use sed command in linux.


sed command is used to perform operations on files. sed is used for replacing strings.


What is sed in linux. sed also has many flags.


sed has several commands.

Conclusion

Hurray !! I hope you have known many things about SED which is known as Stream Editor too. In this article, we have discussed the examples of SED Commands with the explanations.

If you have any questions, suggestions, feedback please don’t hesitate to write them in the comment box below because it will help us to improve or correct our contents. Thanks, Happy FOSS Computing 🙂

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.