The Linux command sed
stands for “Stream Editor” and is used to read text streams, search for specific patterns, and modify or replace them. It’s a highly efficient tool for managing large text files, making it essential for developers and system administrators. In this post, we’ll explore the basic usage of the sed
command and its key options.
Table of Contents
Basic Structure of the Linux Command sed
The sed
command is generally used in the following format:
sed [options] 'command' file
ShellScriptHere, options are optional, while the command defines what action sed
will perform. The most commonly used command is s
(substitute), which finds a specific text pattern and replaces it with another.
Text Substitution
sed 's/old-text/new-text/' filename
ShellScriptThis command finds ‘old-text’ in the file and replaces it with ‘new-text’. If ‘old-text’ appears multiple times in a line, by default only the first occurrence in each line is replaced.
Hello, Jacob! Hello, Mary!
Hello, Jemma! Hello, Davidson!
Hello, John! Hello, Jack!
PlaintextIf you use the command s/Hello/Hi/
, only the first occurrence of “Hello” in each line will be replaced with “Hi.”
The g (global) Command
By default, the sed
command only changes the first matching pattern in each line. However, if you want to replace all matching patterns in a line, you need to use the g
command.
sed 's/old-text/new-text/g' filename
ShellScriptBy adding the g
command at the end, all occurrences of ‘old-text’ in a line are replaced with ‘new-text’. Below is an example where every “Hello” in the example.txt
file is replaced with “Hi.”
Key Options You Can Use with sed
The sed
command is incredibly powerful, offering a variety of options. Here, we’ll introduce some frequently used ones.
The -i (in-place) Option
By default, sed
doesn’t modify the original file but outputs the changes. However, by using the -i
option, you can modify the original file directly.
sed -i 's/old-text/new-text/' filename
ShellScriptThis command directly replaces ‘old-text’ with ‘new-text’ in the original file. Note that when using the -i
option, the file is immediately modified, so it’s a good idea to create a backup beforehand.
The -n (silent) Option
By default, the sed
command outputs all lines of the file. However, if you use the -n
option, it only outputs the lines where you’ve used the p
(print) command.
sed -n 's/old-text/new-text/p' filename
ShellScriptIf you only use the -n
option, the modified content won’t be printed. But when used together with the p
command, only the lines where ‘old-text’ is replaced by ‘new-text’ will be printed.
The -e (multi-command) Option
When you need to execute multiple commands in sed
, you can use the -e
option.
sed -e 's/text1/text2/' -e 's/text3/text4/' filename
ShellScriptThis command first replaces ‘text1’ with ‘text2’, and then replaces ‘text3’ with ‘text4’. The -e
option is useful when executing multiple commands in sequence.
Practical Examples
Modifying Specific Lines
If you want to modify only specific lines in a text file, you can specify the line number.
sed '2s/old-text/new-text/' filename
ShellScriptThis command changes ‘old-text’ to ‘new-text’ only on the second line of the file. By specifying the line number, you can easily modify a specific part of a large file.
Using Regular Expressions
sed
supports regular expressions, allowing you to define more flexible patterns. For example, if you want to find and replace only numbers, you can use the following command:
sed 's/[0-9]/#/g' filename
ShellScriptThis command replaces all numbers in the file with #
. Regular expressions are one of sed
‘s most powerful features, enabling easy handling of complex pattern matching.
Backing Up Files While Modifying Them
When using the -i
option, you can create a backup file by specifying an extension.
sed -i.bak 's/old-text/new-text/' filename
ShellScriptThis command creates a backup of the original file with the .bak
extension before modifying it. It’s recommended to back up important files to avoid losing data due to accidental modifications. In this case, the original file will be saved as filename.bak
.
Precautions When Using sed
- Backup Recommended for File Modifications: When using the
-i
option to modify a file directly, always create a backup. Oncesed
is executed, it’s difficult to reverse the changes, so take extra care when dealing with important data. - Be Careful with Regular Expressions: When using regular expressions in
sed
, always ensure that the pattern behaves as expected. Special characters like.
or*
have specific meanings in regular expressions, so use\
to escape them if you want them treated as plain text. - Combine with Other Commands: While
sed
is powerful on its own, you can combine it with other commands likegrep
orawk
to create even more robust scripts.
Summary
The Linux command sed
is a powerful tool for modifying files and processing text data in Linux. It can handle a wide range of tasks, from simple text substitutions to complex transformations using regular expressions. By understanding the basic usage and key options discussed in this post, you can process text more efficiently.
Additionally, always practice safe habits by backing up your data, and consider using sed
with other utilities to create more powerful automation scripts. sed
is an indispensable tool in almost every Linux task involving text files.