G-D00XZP85S3 These 10 Sed Examples Will Make You a Linux Power User - KangMus Official
Skip to content Skip to sidebar Skip to footer

These 10 Sed Examples Will Make You a Linux Power User

Editing text files and terminal output is an everyday job for those who administer Linux machines. Command-line utilities like sed allow a user to modify and change the content of a text file right from the terminal window.

In this article, we will discuss the sed command in detail, along with some essential examples that demonstrate the power of the sed utility in Linux.

What Is the sed Command?

The sed command, which is an acronym for Stream Editor, is a command-line tool that allows Linux users to perform text-based operations on files and terminal outputs. Using sed, users can find and replace specific words in a text, display a certain section of the output, and edit text files without opening them.

The three basic operations supported by the sed command are:

  1. Insertion
  2. Deletion
  3. Substitution (Find and replace)

Advanced users can also implement regular expressions with the sed command to edit text streams more efficiently.

The basic syntax of the command is:

sed [options] [pattern] [filepath]

...where options are the various functionalities of the command, pattern is the regular expression or the script that you want to match, and filepath is the path to the text file that contains the text.

10 Examples of the Linux sed Command

If you plan to become a regular Linux user, knowing how to edit files, search and replace specific words, and filter the terminal output might be useful to you. This section covers some examples of the sed command that will definitely turn you into a Linux power user.

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

This is a demo text file.
It is an amazing file that will help us all.
The sed command is also great for stream editing.
Want 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 named as textfile.
This is a apple.
This is a orange.

1. View a Range of Lines

Linux commands such as head and tail output the first or the last ten lines of a text file. But what if you want to get the content between two specific lines in a file? In such situations, the sed command might come in handy.

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

sed -n '3,5p' textfile.txt

The -n flag prevents sed from displaying the pattern space at the end of each cycle. You can also use the --quiet and --silent options instead of -n. The p argument stands for print and is used to display the matched lines to the user.

Executing the aforementioned command on the example file produces the following output.

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

To output the entire file content except for the specified range, use the d flag instead of p in the command:

sed '3,5d' textfile.txt

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

This is a demo text file.
It is an amazing file that will help us all.
This is the third general line in the file.
This file is named as textfile.
This is a apple.
This is a orange.

2. Display Non-Consecutive Lines

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

sed -n -e '1,2p' -e '5,6p' textfile.txt

Output:

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

The -e flag helps in executing multiple actions using a single command.

3. Insert Space Between Lines

If for any reason you want to insert empty lines between each line in a text file, use the G argument with the default sed command.

sed G textfile.txt

To insert multiple empty lines in the output, pass multiple G arguments separated by the semi-colon (;) character.

sed 'G;G' textfile.txt

4. Replace a Word in a Text File

If you want to replace each occurrence of a specific word with some other word, use the s and g arguments with the command. The basic syntax for substituting words using the sed command is:

sed s/originalword/replaceword/g filename

Using the above-mentioned syntax, you can 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 command is used to replace the matched content with the specified replacement content.

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

sed s/amazing/super/g2 textfile.txt

If you want to ignore character cases while substituting words, use gi instead of g, where i stands for ignore case.

sed s/Amazing/super/gi textfile.txt

Related: Using Vi? Here's How to Open a File Then Save and Quit

5. Substitute Words Inside a Range

You can also substitute words inside a specific range.

sed '2,5s/amazing/super/g' textfile.txt

6. Perform Multiple Substitutions at Once

If you want to perform two or more substitutions at once, just separate the commands with the semi-colon (;) character.

sed 's/amazing/super/g;s/command/utility/gi' textfile.txt

The system will display the following output.

This is a demo text file.
It is an super file that will help us all.
The sed utility is also great for stream editing.
Want 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 named as textfile.
This is a apple.
This is a orange.

7. Replace Words Only If a Match Is Found

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

sed -e '/orange/ s/a/an/g' textfile.txt

Issuing the above-mentioned command will output:

This is a demo text file.
It is an super file that will help us all.
The sed utility is also great for stream editing.
Want 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 named as textfile.
This is a apple.
This is an orange.

Note that the word in the line This is a apple wasn't replaced as the system didn't find the word orange in it.

8. Substitute Words Using Regular Expressions

For those who know how to use regular expressions, performing operations on strings using the sed command becomes a lot easier. You can implement regular expressions to enhance the power of the command.

To replace all occurrences of the word Amazing or amazing with super:

sed -e 's/[Aa]mazing/super/g' textfile.txt

Similarly, you can also use advanced regular expressions to execute specific operations using the sed command.

9. Pipe sed With Other Commands

You can chain sed with other Linux commands as well. For example, you can pipe the lspci command with sed to add empty spaces between lines in the output.

lspci | sed G

To replace specific words in the output of the ip route show command:

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

The aforementioned command substitutes the word source in place of the original word src.

Related: How to Use the Find Command to Search for Files in Linux

10. Edit and Backup the Original File

When you are working with system files, backing up the original file while making changes is important. This will help you in reverting the changes in case something breaks.

To back up the original file using sed, use the -i flag in the command.

sed -i'.backup' 's/amazing/super/g' textfile.txt

A new file will be created with the name textfile.txt.backup. You can check that the two files are different using the diff command.

diff textfile.txt textfile.txt.backup

Editing Strings in Linux With sed

Sometimes, while you're working with text files on the terminal, formatting and editing the output for better readability becomes a must. Sed and awk are command-line utilities in Linux that allow a user to work efficiently with text files by dividing the data into separate lines.

Many users have a hard time memorizing the arguments and flags of the sed command since there are a lot of them that are available to use. Knowing how to get command-line manuals for any Linux command will help you in getting out of such situations easily.



source https://www.makeuseof.com/sed-examples/

Post a Comment for "These 10 Sed Examples Will Make You a Linux Power User"