These 10 Sed Examples Will Make You a Linux Energy Person

Editing text files and terminal output is an everyday task for those who manage Linux computers. Command line programs such as sed allow a user to change the contents of a text file directly from the terminal window.

In this article, we'll discuss the sed command in detail, along with some key examples that demonstrate the power of the sed utility on Linux.

What is the sed command?

The sed command, which is an acronym for Stream editoris a command line tool that enables Linux users to perform text-based operations on files and terminal output. Sed allows users to find and replace specific words in a text, view a specific section of output, and edit text files without opening them.

The three basic operations supported by the sed command are:

  1. Insert

  2. Deletion

  3. Substitution (search and replace)

Advanced users can also use the sed command to implement regular expressions to manipulate streams of text more efficiently.

The basic syntax of the command is:

sed (options) (pattern) (file path)

…Where Options are the different functions of the command, template is the regular expression or script you want to match, and File path is the path to the text file that contains the text.

10 examples of the Linux sed command

If you are planning to become a regular Linux user, knowing how to edit files, find and replace specific words, and filter terminal output can be helpful. This section contains some examples of the sed command that will definitely make you a Linux power user.

We will be using the following text file for demonstration in the mail.

This is a demo text file.
It's an amazing file that will help all of us.
The sed command is also great for stream editing.
Would you like to learn how to use the command?
This is another line in the file.
This is the third general line in the file.
This file is known as a text file.
It's an apple.
It's an orange.

1. Display a series of lines

Linux commands like head and tail print the first or last ten lines of a text file. But what if you want to get the content between two specific lines in a file? The sed command can be useful in such situations.

Output of the content between lines 3 and 5 of the file textfile.txt::

sed -n & # 39; 3,5p & # 39; textfile.txt

The -n The flag prevents sed from displaying the pattern space at the end of each cycle. You can use that too –calm and –Quietly Options instead of -n. The p Argument stands for to press and is used to show the user the matching lines.

Running the above command in the sample file produces the following output.

The sed command is also great for stream editing.
Would you like to learn how to use the command?
This is another line in the file.

Use the key to output all of the file contents except the specified area d Flag instead p in the command:

sed & # 39; 3,5d & # 39; textfile.txt

The d flag deletes the matching strings from the output and displays the rest of the content.

This is a demo text file.
It's an amazing file that will help all of us.
This is the third general line in the file.
This file is known as a text file.
It's an apple.
It's an orange.

2. Show non-consecutive lines

To print non-consecutive lines between multiple areas in the file:

sed -n -e & # 39; 1,2p & # 39; -e & # 39; 5,6p & # 39; textfile.txt

Output:

This is a demo text file.
It's an amazing file that will help all of us.
This is another line in the file.
This is the third general line in the file.

The -e Flag helps To run multiple actions with a single command.

3. Insert a space between the lines

If for some reason you want to insert blank lines between each line of a text file, use the G Argument with the standard sed command.

sed G textfile.txt

To include multiple blank lines in the output, pass multiple G Arguments separated by the semicolon ((;;) Character.

sed & # 39; G; G & # 39; textfile.txt

4. Replace a word in a text file

If you want to replace every occurrence of a particular word with a different word, use the s and G Arguments with the command. The basic syntax for replacing words with the sed command is:

sed s / originalword / replaceword / g filename

You can use the above syntax to replace the word amazing With Super in the file textfile.txt::

sed s / amazing / super / g textfile.txt

The s Argument denotes Substitution and the G The command is used to replace the matching content with the specified replacement content.

To replace the second occurrence of the word with sed, pass a number to G Dispute. In this case:

sed s / amazing / super / g2 textfile.txt

If you want to ignore character cases when replacing words, use gi Instead of G, Where I stands for to ignore Case.

sed s / Amazing / super / gi textfile.txt

Related: Using Vi? How to open, save, and exit a file

5. Replace words within a range

You can also replace words within a certain range.

sed & # 39; 2.5s / amazing / super / g & # 39; textfile.txt

6. Perform multiple replacements at the same time

If you want to do two or more replacements at the same time, just separate the commands semicolon ((;;) Character.

sed & # 39; s / amazing / super / g; s / command / utility / gi & # 39; textfile.txt

The system displays the following output.

This is a demo text file.
It's a great file that will help all of us.
The sed utility is also great for stream editing.
Would you like to learn how to use the utility?
This is another line in the file.
This is the third general line in the file.
This file is known as a text file.
It's an apple.
It's an orange.

7. Only replace words when a match is found

You can also use the sed command to replace a word only if a specific match is found on the line. For example, to replace the word a With a if the word orange is present in the line:

sed -e & # 39; / orange / s / a / an / g & # 39; textfile.txt

When you enter the above command it will output:

This is a demo text file.
It's a great file that will help all of us.
The sed utility is also great for stream editing.
Would you like to learn how to use the utility?
This is another line in the file.
This is the third general line in the file.
This file is known as a text file.
It's an apple.
It's an orange.

Notice the word a In the queue It's an apple was not replaced because the system could not find the word orange inside.

8. Replace words with regular expressions

For those who know how to use regular expressions, performing operations on strings with the sed command becomes much easier. You can implement regular expressions to improve the performance of the command.

To replace all occurrences of the word Amazing or amazing With Super::

sed -es / (Aa) mazing / super / g & # 39; textfile.txt

Similarly, you can also use advanced regular expressions to perform certain operations with the sed command.

9. Pipe sed with other commands

You can also chain sed with other Linux commands. For example, you can use the pipe lspci Command with sed to add spaces between lines in output.

lspci | sed G.

To replace certain words in the output of the Show IP route Command:

ip route show | sed s / src / source / g

The above command replaces the word source instead of the original word src.

Related topics: Using the Find Command to Find Files on Linux

10. Edit and save the original file

When working with system files, it is important to back up the original file while you make changes. This will help you roll back the changes in case something breaks.

Use the option to save the original file with sed -I Flag in command.

sed -i & # 39; .backup & # 39; s / amazing / super / g & # 39; textfile.txt

A new file with the name is created textfile.txt.backup. You can check if the two files are different diff Command.

diff textfile.txt textfile.txt.backup

Editing strings under Linux With sed

While working with text files on the terminal, it is sometimes a must to format and edit the output for better readability. Sed and awk are command line utilities on Linux that allow a user to work efficiently with text files by dividing the data into separate lines.

Many users find it difficult to remember the arguments and flags of the sed command because many of them are available. Knowing how to get command line guides for each Linux command can help you overcome such situations with ease.

7 Ways to Get Command Line Help on Linux

All the essential commands for learning Linux commands from the command line

Continue reading

About the author

Deepesh Sharma
(39 articles published)

Deepesh is the junior editor for Linux at MUO. He has been writing informational content on the Internet for over 3 years. In his spare time he enjoys writing, listening to music and playing the guitar.

More
From Deepesh Sharma

Subscribe to our newsletter

Sign up for our newsletter to receive tech tips, reviews, free e-books, and exclusive offers!

One more step …!

Please confirm your email address in the email we just sent you.

Leave a Reply

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