The sed
(Stream Editor) command is a powerful tool for performing text transformations and manipulations on streams of text, such as files or input from pipelines. This reference provides commonly used sed
commands and examples to help you efficiently edit and process text files.
The general syntax for using sed
is:
sed [options] 'command(s)' file(s)
Or for inline commands:
sed [options] -e 'command1' -e 'command2' file(s)
Option | Description |
---|---|
-e 'command' |
Add the script to the commands to be executed. |
-f scriptfile |
Execute the script in the specified file. |
-i[SUFFIX] |
Edit files in-place (optionally creating a backup with the given suffix). |
-n |
Suppress automatic printing of pattern space. |
-r or -E |
Use extended regular expressions in the script. |
-s |
Treat files as separate rather than as a single continuous long stream. |
Command | Description |
---|---|
s/old/new/flags |
Substitute new for the first occurrence of old in a line. |
p |
Print the current pattern space. |
d |
Delete the pattern space; start next cycle. |
q |
Quit sed ; no further processing. |
a\ text |
Append text after the current line. |
i\ text |
Insert text before the current line. |
c\ text |
Replace the current line with text . |
n |
Read next line into pattern space. |
h |
Copy pattern space to hold buffer. |
g |
Get hold buffer content and replace pattern space. |
G |
Append hold buffer content to pattern space. |
x |
Exchange the pattern space with the hold buffer. |
= |
Print the current line number. |
s///
)Flag | Description |
---|---|
g |
Global replacement in a line (replace all occurrences). |
p |
Print the line if a replacement was made. |
w file |
Write the line to file if a replacement was made. |
I |
Case-insensitive search. |
n |
Replace the n -th occurrence in a line. |
Addresses specify which lines of input to apply commands to.
number
- Match only the specified line number.$
- Match the last line./pattern/
- Match lines that contain the regular expression pattern
.first~step
- Starting at line first
, match every step
lines.addr1,addr2
- Match range from addr1
to addr2
, inclusive.Command | Description |
---|---|
sed 's/old/new/' file |
Replace first occurrence of old with new in each line. |
sed 's/old/new/g' file |
Replace all occurrences of old with new in each line. |
sed -i 's/old/new/g' file |
Edit the file in-place, replacing all occurrences. |
sed '/pattern/d' file |
Delete lines matching pattern . |
sed -n '/pattern/p' file |
Print only lines matching pattern . |
sed -n '5,10p' file |
Print lines from 5 to 10. |
sed '1d' file |
Delete the first line. |
sed '$d' file |
Delete the last line. |
sed '1i\New Header' file |
Insert "New Header" at the beginning of the file. |
sed 's/^/prefix_/' file |
Add "prefix_" at the beginning of each line. |
sed 's/$/_suffix/' file |
Add "_suffix" at the end of each line. |
sed 's/^\(.\{5\}\)/\1.../' file |
After first 5 characters of each line, insert "...". |
sed -n '=' file |
Print line numbers. |
sed '/^$/d' file |
Delete empty lines. |
sed 's/\(.*\)/\U\1/' file |
Convert text to uppercase. |
sed 's/\(.*\)/\L\1/' file |
Convert text to lowercase. |
sed 's/^\s\+//' file |
Remove leading whitespace. |
sed 's/\s\+$//' file |
Remove trailing whitespace. |
By default, sed
operates on a per-line basis. To perform multi-line operations, use the following commands:
N
- Read the next line into the pattern space (separated by a newline).D
- Delete up to the first newline in pattern space and start next cycle.H
- Append the pattern space to the hold buffer with a newline.G
- Append the hold buffer to the pattern space with a newline.sed 'N;s/\n/ /' file
This reads the next line into the pattern space and replaces the newline character with a space, effectively joining every two lines.
sed
supports regular expressions for pattern matching:
^
- Start of line$
- End of line.
- Any single character*
- Zero or more of the preceding character\+
- One or more of the preceding character (requires -r
or -E
option)\?
- Zero or one of the preceding character (requires -r
or -E
option)\|
- Alternation (logical OR, requires -r
or -E
)\(\)
- Grouping\1, \2, ...
- Back-references to captured groups[abc]
- Character class matching a
, b
, or c
[^abc]
- Negated character classsed
provides a hold space (buffer) for advanced text processing:
h
- Copy pattern space to hold space (overwrites hold space).H
- Append pattern space to hold space (with a newline).g
- Replace pattern space with hold space.G
- Append hold space to pattern space.x
- Exchange pattern space with hold space.sed 'N;swap_lines' file
# Define the swap_lines script:
swap_lines='
h # Copy pattern space (line1\nline2) to hold space
s/.*\n// # Remove line1, keep line2 in pattern space
x # Exchange pattern space and hold space
s/\n.*// # Remove line2, keep line1 in pattern space
G # Append hold space (which now contains line2) to pattern space
'
sed
script to prevent shell interpretation.-f
option with a script file.-i
), especially without backups.sed
commands on sample data before applying to important files.-r
or -E
option for more concise patterns.