Mastering the sed Command in Linux: A Comprehensive Guide
The sed
command in Linux is a stream editor and a versatile text-processing tool. It allows users to efficiently transform text by parsing and modifying data from files or input streams. Whether you need to replace words, remove lines, or handle patterns, sed
simplifies repetitive tasks and boosts productivity.
In this blog, we will explore the basics of sed
command, understand its syntax, and share practical examples to show how it makes text processing easier.
Table of Contents:
- What is sed command in Linux?
- Syntax of the sed Command
- Common sed Operations and Practical Examples
- Advanced Features of sed Command
- Practical Use Cases
- Key Points to Remember
What is sed Command in Linux?
The sed
command is part of the basic set of tools available in every Linux installation. sed
stands for stream editor and is a non-interactive text editor. This means that changes are not made directly to the file being edited. Instead, sed
reads the file line by line, processes the content, and then outputs the result, which can be redirected to a file or another process.
With sed
, you can perform operations like:
- Select text (using patterns and regular expressions)
- Substitute text (replace occurrences of text with other text)
- Add lines (insert text at specific positions)
- Delete lines (remove lines that match specific criteria)
- Modify (or preserve) the original file (using options like
-i
to edit in place or redirecting output to a new file)
Syntax of the sed Command
sed [options] 'command' file
- options: Flags that modify the behaviour of sed.
- command: The operation
sed
performs on the input. This can be a substitution, deletion, insertion, etc. - file: The file you want to edit or process. If no file is specified,
sed
works on the input from standard input (stdin).
Common Options:
-e
: To add multiple editing commands.-i
: To edit files in place (without creating a backup).-n
: To suppress default output (used withp
command to print specific lines).-f
: To use a script file with multiplesed
commands.
Common sed Operations and Practical Examples
1. Search and Replace
The most common use of sed
is to find and replace text in a file or stream.
(i). Replace a word:
sed 's/oldword/newword/' file.txt
This replaces the first occurrence of oldword
with newword
on each line.
(ii). Replace all occurrences:
sed 's/oldword/newword/g' file.txt
The g
flag ensures all occurrences of oldword
are replaced.
(iii). Replace in-place:
To save changes directly in the file:
sed -i 's/oldword/newword/g' file.txt
Note: On macOS, usesed -i ''
instead ofsed -i
.
2. Delete Lines
(i). Delete a specific line:
sed '5d' file.txt
This command deletes the 5th line of the file.
(ii). Delete a range of lines:
sed '3,7d' file.txt
This command deletes lines 3 to 7.
(iii). Delete lines containing a pattern:
sed '/pattern/d' file.txt
Removes all lines containing pattern
.
3. Print Lines
(i). Print a specific line:
sed -n '5p' file.txt
Prints the 5th line.
(ii). Print a range of lines:
sed -n '3,7p' file.txt
Prints lines 3 to 7.
(iii). Print lines matching a pattern:
sed -n '/pattern/p' file.txt
Prints lines containing pattern
.
4. Insert and Append Text
(i). Insert text before a specific line:
sed '3i\This is inserted text' file.txt
Inserts This is inserted text
before line 3.
(ii). Append text after a specific line:
sed '3a\This is appended text' file.txt
Appends This is appended text
after line 3.
5. Use sed
in Pipelines
sed
works seamlessly with other commands in Linux pipelines.
(i). Convert all text to uppercase:
echo "hello world" | sed 's/.*/\U&/'
(ii). Add line numbers to a file:
cat file.txt | sed = | sed 'N;s/\n/\t/'
Advanced Features of sed Command
1. Using Regular Expressions
sed
supports regular expressions, making it a powerful tool for pattern matching.
Replace only lines starting with "test":
sed '/^test/s/oldword/newword/' file.txt
Replace a word at the end of a line:
sed 's/word$/newword/' file.txt
2. Using Multiple Commands
You can apply multiple commands using -e
or a script file.
With -e
:
sed -e 's/oldword/newword/' -e '3d' file.txt
From a script file:
Create a file commands.sed
:
s/oldword/newword/
3d
Run:
sed -f commands.sed file.txt
3. Backreferences
Use backreferences to match and reuse parts of the pattern.
Swap two words:
echo "first second" | sed 's/\(first\) \(second\)/\2 \1/'
Practical Use Cases
1. Update Configuration Files
sed -i 's/timeout=30/timeout=60/' config.txt
2. Bulk Rename Files
ls *.txt | sed 's/\.txt/.bak/' | xargs -I {} mv {} {}
3. Extract Specific Data from Logs
grep "ERROR" logs.txt | sed 's/^.*ERROR: //'
Key Points to Remember
- Use
sed
for non-interactive text editing. - Combine
sed
with regular expressions for complex patterns. - Use
-i
with caution as it edits files in place. - Adjust the
-i
syntax for compatibility between Linux and macOS.
Conclusion
The sed
command is a versatile and essential tool for Linux users. With its ability to process text efficiently, it’s ideal for tasks ranging from simple find-and-replace to complex text transformations. Whether you are automating file updates or parsing logs, sed
can simplify your workflows. Explore sed
further by experimenting with its options and combining it with other Linux commands to unlock its full potential.
#1 Solution for Logs, Traces & Metrics
APM
Kubernetes
Logs
Synthetics
RUM
Serverless
Security
More