sed
(Stream Editor) is one of the most powerful text processing tools in Linux. Whether you're performing basic text replacement, deleting lines, or transforming entire files, sed
provides a variety of commands and flags that can be used to manipulate text streams effectively.
In this post, weβll dive into sed
commands and their flags, explore common use cases, and look at examples to make you a sed
master! π οΈ
π― Common sed
Commands & Flags Explained
1. Substitution Command (s
)
The substitution command is by far the most commonly used in sed
. It searches for a pattern and replaces it with a replacement string.
Syntax:
sed 's/pattern/replacement/flags' file
Example: Basic Substitution
sed 's/foo/bar/' file.txt
- This replaces the first occurrence of
foo
withbar
in each line offile.txt
.
Example: Global Substitution
sed 's/foo/bar/g' file.txt
- Replaces all occurrences of
foo
withbar
in each line.
Example: Case-Insensitive Substitution
sed 's/foo/bar/i' file.txt
- Replaces
foo
withbar
regardless of case (i.e.,Foo
,fOO
, etc., are also replaced).
Example: N-th Occurrence Substitution
sed 's/foo/bar/2' file.txt
- Replaces the second occurrence of
foo
withbar
in each line.
2. Delete Command (d
)
The delete command removes lines that match a given pattern or condition.
Syntax:
sed '/pattern/d' file
Example: Delete Lines with a Specific Word
sed '/error/d' file.txt
- This deletes all lines containing the word
error
fromfile.txt
.
Example: Delete Blank Lines
sed '/^$/d' file.txt
- Deletes blank lines (lines with no content).
Example: Delete Lines Starting with a Number
sed '/^[0-9]/d' file.txt
- Deletes all lines starting with a digit.
3. Print Command (p
)
The print command allows you to print lines matching a given pattern. It is often used with the -n
flag to suppress the default behavior of printing all lines.
Syntax:
sed -n '/pattern/p' file
Example: Print Lines Containing a Specific Word
sed -n '/error/p' file.txt
- This prints only the lines containing the word
error
.
Example: Print Matching Lines with the Next Line
sed -n '/error/{N; p}' file.txt
- Prints the matching lines along with the next line (this uses the
N
command to append the next line to the pattern space).
4. Insert Command (i
)
The insert command adds a line before the specified line number or pattern.
Syntax:
sed 'line_number i\text_to_insert' file
Example: Insert Text Before a Line
sed '2i\This is inserted before line 2' file.txt
- Inserts text before line 2.
Example: Insert Text Before a Matching Pattern
sed '/error/i\This is an error message' file.txt
- Inserts "This is an error message" before every line containing
error
.
5. Append Command (a
)
The append command adds a new line after the specified line number or pattern.
Syntax:
sed 'line_number a\text_to_append' file
Example: Append Text After a Line
sed '2a\This is appended after line 2' file.txt
- Appends text after line 2.
Example: Append Text After a Matching Pattern
sed '/error/a\This is an error resolution step' file.txt
- Appends "This is an error resolution step" after every line containing
error
.
6. Change Command (c
)
The change command replaces the entire content of a line with new text.
Syntax:
sed 'line_number c\new_text' file
Example: Change Content of a Line
sed '2c\This is the new content for line 2' file.txt
- Replaces the content of line 2 with
This is the new content for line 2
.
Example: Replace Lines Matching a Pattern
sed '/error/c\Critical error occurred' file.txt
- Replaces all lines containing
error
withCritical error occurred
.
7. Address Ranges
You can apply sed
commands to a range of lines using either line numbers or patterns.
Syntax for Line Numbers:
sed 'start_line,end_line command' file
Example: Delete Lines 2 to 4
sed '2,4d' file.txt
- Deletes lines 2 to 4 from
file.txt
.
Syntax for Pattern-based Range:
sed '/start_pattern/,/end_pattern/d' file
Example: Delete Lines Between Patterns
sed '/start/,/end/d' file.txt
- Deletes lines between (and including) the first occurrence of
start
andend
.
8. Multiple Commands with -e
Flag
The -e
flag allows you to execute multiple commands sequentially.
Syntax:
sed -e 'command1' -e 'command2' file
Example:
sed -e 's/foo/bar/' -e 's/baz/qux/' file.txt
- First, it replaces
foo
withbar
, then it replacesbaz
withqux
.
9. Suppress Output with -n
Flag
By default, sed
prints every line. The -n
flag suppresses this, allowing you to print only the lines you need.
Syntax:
sed -n 'pattern p' file
Example:
sed -n '/pattern/p' file.txt
- Prints only the lines containing
pattern
.
10. In-Place Editing with -i
Flag
The -i
flag enables in-place editing, meaning the file is modified directly without needing redirection.
Syntax:
sed -i 's/foo/bar/g' file
Example: In-Place Substitution
sed -i 's/foo/bar/g' file.txt
- Replaces all occurrences of
foo
withbar
and saves the changes tofile.txt
.
Example: In-Place Editing with Backup
sed -i.bak 's/foo/bar/g' file.txt
- Creates a backup file
file.txt.bak
before making the substitution.
11. Extended Regular Expressions with -E
Flag
To enable extended regular expressions (ERE) in sed
, use the -E
flag. ERE allows more powerful patterns, such as +
, ?
, and |
.
Syntax:
sed -E 's/[0-9]+/number/' file
Example:
sed -E 's/\b[0-9]{3}\b/NUMBER/' file.txt
- Replaces any 3-digit number with
NUMBER
.
12. Escaping Special Characters
sed
treats certain characters (like /
, &
, and \
) as special. To use them literally, you must escape them with a backslash (\
).
Example with Special Characters:
sed 's/foo/bar\&baz/' file.txt
- Replaces
foo
withbar&baz
(escaping&
).
π Conclusion
sed
is an incredibly versatile tool for text manipulation in Linux. From simple substitutions to complex pattern matching and file modifications, sed
can handle almost any task related to text processing. By mastering the commands and flags covered here, you can greatly enhance your text manipulation skills.
Donβt hesitate to experiment with these commands to solve your text processing needs, and let me know in the comments if you have any questions or need further clarification! π¨βπ»π©βπ»
Happy editing with sed
! βοΈ
Top comments (0)