How to use the command 'sed' (with examples)

How to use the command 'sed' (with examples)

  • Osx
  • December 25, 2023

The sed command is a powerful text editor that allows you to edit text in a scriptable manner. It can be used to perform various operations on text files, such as searching, replacing, inserting, and deleting text. In this article, we will explore different use cases of the sed command with examples.

Use case 1: Replace all occurrences of a word in input lines and print the result to stdout

Code:

command | sed 's/apple/mango/g'

Motivation: This use case is helpful when you want to replace all occurrences of a specific word, such as “apple,” with another word, such as “mango,” in the input lines.

Explanation:

  • command represents the command that generates the input lines.
  • sed 's/apple/mango/g' is the sed command that performs the search and replace operation. Here, s indicates the substitution command, apple is the word to be replaced, mango is the word to replace with, and g indicates a global search and replace (replaces all occurrences).

Example output: If the input lines are:

I have an apple.
I like to eat apple pie.

The output will be:

I have a mango.
I like to eat mango pie.

Use case 2: Execute a script file and print the result to stdout

Code:

command | sed -f path/to/script_file.sed

Motivation: This use case is useful when you have a specific script file containing a sequence of sed commands that you want to execute on the input lines.

Explanation:

  • command represents the command that generates the input lines.
  • sed -f path/to/script_file.sed is the sed command that reads and executes the commands from the specified script file.

Example output: If the script file contains the following command:

s/apple/mango/g

And the input lines are the same as in the previous use case, the output will be:

I have a mango.
I like to eat mango pie.

Use case 3: Replace all occurrences of a word using extended regex and print the result to stdout

Code:

command | sed -E 's/(apple)/\U\1/g'

Motivation: This use case is helpful when you want to perform a search and replace operation using extended regex (regular expression), which provides more powerful pattern matching capabilities.

Explanation:

  • command represents the command that generates the input lines.
  • sed -E 's/(apple)/\U\1/g' is the sed command that performs the search and replace operation using extended regex. Here, -E enables extended regex, (apple) captures the word “apple” as a group, \U\1 converts the captured group to uppercase, and g indicates a global search and replace.

Example output: If the input lines are the same as in the use case 1, the output will be:

I have an APPLE.
I like to eat APPLE pie.

Use case 4: Print just the first line to stdout

Code:

command | sed -n '1p'

Motivation: This use case is useful when you want to extract and print only a specific line from the input lines, such as the first line.

Explanation:

  • command represents the command that generates the input lines.
  • sed -n '1p' is the sed command that selects and prints the first line. Here, -n disables the default printing, and 1p specifies to print the first line.

Example output: If the input lines are:

Line 1
Line 2
Line 3

The output will be:

Line 1

Use case 5: Replace all occurrences of a word in a file and save a backup of the original file

Code:

sed -i bak 's/apple/mango/g' path/to/file

Motivation: This use case is useful when you want to replace all occurrences of a specific word in a file and create a backup of the original file.

Explanation:

  • sed -i bak 's/apple/mango/g' is the sed command that performs the search and replace operation in the specified file and saves a backup of the original file with the extension .bak. Here, -i bak enables in-place editing and specifies the backup file extension.

Example output: If the content of the file is the same as in the use case 1, after executing the command, the file will be modified, and a backup file file.bak will be created. The modified file will have the following content:

I have a mango.
I like to eat mango pie.

Conclusion:

The sed command is a versatile tool for performing text manipulation operations. In this article, we explored various use cases of the sed command with examples, such as replacing words, executing script files, using extended regex, extracting specific lines, and editing files with backups. Understanding these use cases will empower you to efficiently edit text in a scriptable manner using the sed command.

Tags :

Related Posts

How to use the command 'shutdown' (with examples)

How to use the command 'shutdown' (with examples)

The ‘shutdown’ command is a powerful tool that allows users to shut down, restart, hibernate, log off, or abort a shutdown sequence on a machine.

Read More
How to use the command fsck (with examples)

How to use the command fsck (with examples)

The fsck command is used to check the integrity of a filesystem or repair it.

Read More
How to use the command 'q' (with examples)

How to use the command 'q' (with examples)

The ‘q’ command allows users to execute SQL-like queries on .

Read More