The sed
command in Linux is a powerful text-processing tool, often described as a "stream editor." It allows users to search, find, replace, insert, or delete text in files or streams. While it may seem complex at first, sed
is an essential tool for automating tasks and handling large amounts of data efficiently. In this article, we’ll explore the basics of sed
and include plenty of examples to help you get started.
What is sed
?
sed
, short for stream editor, reads text from standard input or a file, performs text transformations, and outputs the result. Unlike traditional text editors, sed
operates non-interactively, meaning it processes text in a stream, making it ideal for scripting and automating repetitive tasks.
How to Use sed
The basic syntax of the sed
command is as follows:
sed [options] 'script' file
-
[options]
: Flags to modifysed
behavior (e.g.,-i
for in-place editing). -
'script'
: Instructions for processing text (e.g., substitution, deletion). -
file
: Input file (optional;sed
can also read from standard input).
Common Use Cases and Examples
1. Substituting Text
The s
command is used for substitution. To replace all occurrences of "foo" with "bar":
sed 's/foo/bar/' input.txt
-
Example Output:
If
input.txt
contains "foo is great", the result will be "bar is great".
To replace text globally (all occurrences in a line), use the g
flag:
sed 's/foo/bar/g' input.txt
2. In-Place Editing
Edit a file directly without creating a new file using the -i
flag:
sed -i 's/foo/bar/g' input.txt
This replaces "foo" with "bar" across all lines of input.txt
.
3. Deleting Lines
To delete specific lines, use the d
command:
sed '2d' input.txt
-
Example Output:
This deletes the second line of
input.txt
.
To delete all blank lines:
sed '/^$/d' input.txt
4. Printing Specific Lines
Print only specific lines using the p
command:
sed -n '3p' input.txt
-
Example Output:
This displays only the third line of
input.txt
.
5. Inserting or Appending Text
To insert text before a specific line, use the i
command:
sed '2i\Inserted line' input.txt
This adds "Inserted line" before the second line.
To append text after a specific line, use the a
command:
sed '2a\Appended line' input.txt
6. Replacing Text on Matching Lines
Replace text only on lines that match a pattern:
sed '/pattern/s/foo/bar/' input.txt
- Example: Replace "foo" with "bar" only in lines containing "pattern".
7. Working with Ranges
To apply commands to a range of lines:
sed '2,4d' input.txt
This deletes lines 2 through 4 in input.txt
.
Or, you can match based on patterns:
sed '/start/,/end/d' input.txt
This deletes all lines between "start" and "end" (inclusive).
Combining Multiple Commands
You can chain multiple commands using a semicolon:
sed 's/foo/bar/; 2d' input.txt
This replaces "foo" with "bar" and deletes the second line.
Alternatively, you can use the -e
flag:
sed -e 's/foo/bar/' -e '2d' input.txt
Why Use sed
?
sed
shines when handling repetitive tasks, processing large files, or automating text manipulation in scripts. Whether you’re a system administrator, developer, or data analyst, sed
can save you time and effort.
Conclusion
The sed
command is a versatile tool in the Linux world. Its ability to manipulate text efficiently makes it invaluable for anyone working with Linux. Start simple, experiment with the examples above, and soon you’ll be wielding sed
like a pro.