Commonly Used Sed Command in Linux with Examples

Commonly Used 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?

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.

Amazon Affiliate

Ubuntu Linux Unleashed 2021 Edition

This new edition has been thoroughly updated by a long-time Ubuntu user and early community leader to reflect the exciting new Ubuntu 20.04 LTS release, with a forthcoming online update mid-2021, at the halfway mark before the next LTS release comes out.

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

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.

Leave a Comment

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