How to use the command 'sed' (with examples)
The sed
command (short for “stream editor”) is a powerful utility that allows users to edit text in a scriptable manner. It reads input line by line, applies specified operations to the text, and outputs the results. It is often used in conjunction with pipes (|
) to process and transform text data.
Use case 1: Replace text occurrences (global substitution)
Code:
command | sed 's/apple/mango/g'
Motivation:
The motivation for using this example is to illustrate how to replace all occurrences of a word in a text file and print the modified text to the standard output. This can be useful when you want to make bulk changes to a file without manually editing each occurrence.
Explanation:
command
represents the command that generates the input text. This can be any command or process that produces text as output.sed
is the command itself.'s/apple/mango/g'
is the command argument. Thes
indicates a substitution operation.apple
is the search pattern to be replaced, andmango
is the replacement pattern. Theg
flag stands for global, meaning all occurrences of the search pattern will be replaced.
Example output:
Original Text:
I have an apple.
She has an apple pie.
Modified Text:
I have a mango.
She has a mango pie.
Use case 2: Execute a script file
Code:
command | sed -f path/to/script.sed
Motivation:
The motivation for using this example is to showcase how to execute a script file using the sed
command. This can be useful when you have a collection of operations that need to be applied to a text file and you want to store them in a separate script for modularity and reusability.
Explanation:
command
represents the command that generates the input file. This can be any command or process that produces a file as output.sed
is the command itself.-f path/to/script.sed
is the command argument. The-f
option specifies that the script to be executed is stored in a file.path/to/script.sed
represents the path to the script file.
Example output:
This is the output of the script execution.
Use case 3: Print just the first line
Code:
command | sed -n '1p'
Motivation:
The motivation for using this example is to demonstrate how to print just the first line of a text file using the sed
command. This can be useful when you want to quickly extract specific lines from a file without opening it manually.
Explanation:
command
represents the command that generates the input file. This can be any command or process that produces a file as output.sed
is the command itself.-n
is an option that tellssed
to suppress automatic printing of lines. This is necessary to only display the lines we explicitly instructsed
to print.'1p'
is the command argument.1
represents the line number, andp
is the command to print the line.
Example output:
This is the first line of the file.
Conclusion:
The sed
command is a versatile utility for editing text files in a scriptable manner. It provides a wide range of functionalities, including global substitution, executing scripts, and selecting specific lines from a file. By mastering the various use cases of sed
, users can efficiently process and manipulate text data.