Cheatsheet - sed
sed
(Stream Editor) processes text line by line from an input stream or a file. By default, it prints the processed output to standard output without changing the original file.
Basic Syntax
sed [OPTIONS] 'script' [input-file]
sed [OPTIONS] -e 'script1' -e 'script2' [input-file]
sed [OPTIONS] -f script-file [input-file]
Command-Line Options
Option | Description |
---|---|
-n , --quiet |
Suppress the default behavior of printing every line. Use with the p command to print only specific lines. |
-i [suffix] |
In-place edit. Modifies the file directly. If a suffix is provided (e.g., -i.bak ), a backup of the original file is created. |
-e script |
Add the script (command) to be executed. Useful for running multiple commands. |
-f file |
Read sed commands from a specified file instead of the command line. |
-E , -r |
Use Extended Regular Expressions (ERE) instead of Basic Regular Expressions (BRE). Simplifies some patterns. |
Addressing: Specifying Which Lines to Act On
If no address is specified, the command applies to every line.
Address | Description | Example |
---|---|---|
number |
Act on a specific line number. | sed '3d' file (deletes line 3) |
$ |
Act on the last line of the file. | sed '$d' file (deletes the last line) |
/pattern/ |
Act on any line matching the regular expression pattern . |
sed '/error/d' file (deletes lines with "error") |
start,end |
Act on a range of lines from start to end (inclusive). |
sed '5,10d' file (deletes lines 5 through 10) |
num,+N |
Act on line num and the next N lines. |
sed '5,+3d' file (deletes lines 5, 6, 7, 8) |
/start/,/end/ |
Act on a range from the first line matching /start/ to the first line matching /end/ . |
sed '/BEGIN/,/END/d' file |
addr! |
Invert the address. Act on all lines that do not match the address. | sed '/^#/!d' file (deletes all lines not starting with # ) |
Core Commands
s
- Substitute
This is the most common command in sed
.
Syntax: 's/pattern/replacement/flags'
Flag | Description |
---|---|
g |
Global. Replace all occurrences of the pattern on the line, not just the first one. |
i |
Case-insensitive matching (GNU sed extension). |
N (number) |
Replace only the Nth occurrence of the pattern on the line. |
p |
Print the line if a substitution was made. Often used with -n . |
w file |
Write the line to file if a substitution was made. |
Substitution Examples:
# Replace the first 'cat' with 'dog' on each line
sed 's/cat/dog/' file
# Replace ALL 'cat's with 'dog' on each line
sed 's/cat/dog/g' file
# Replace 'cat' with 'dog' only on line 10
sed '10s/cat/dog/' file
# Replace 'cat' with 'dog' only on lines containing 'animal'
sed '/animal/s/cat/dog/g' file
# Use a different delimiter (#) when the pattern contains slashes
sed 's#/var/log#/usr/share/logs#g' file
# & refers to the entire matched pattern
# Add parentheses around numbers
sed 's/[0-9][0-9]*/(&)/g' file
# Use backreferences (\1, \2) with capturing groups \(...\)
# Swap two words
sed 's/\(first\)\s\(second\)/\2 \1/' file
d
- Delete
Deletes the entire line matching the address.
# Delete line 5
sed '5d' file
# Delete all lines containing 'debug'
sed '/debug/d' file
# Delete all blank lines
sed '/^$/d' file
p
- Print
Prints the line. Almost always used with the -n
option to avoid duplicate output.
# Print only lines 1 through 5 (like head)
sed -n '1,5p' file
# Print only lines containing 'CRITICAL' (like grep)
sed -n '/CRITICAL/p' file
i
, a
, c
- Insert, Append, Change
These commands insert, append, or change entire lines.
Syntax: address [command] \
text to insert/append/change
# Insert text before line 4
sed '4i \
This line will be inserted before line 4.' file
# Append text after the last line
sed '$a \
-- End of File --' file
# Change (replace) the content of line 3
sed '3c \
This is the new content for line 3.' file
Practical Recipes
# In-place edit a file, creating a backup with .bak extension
sed -i.bak 's/old_value/new_value/g' config.conf
# Delete trailing whitespace from all lines
sed -i 's/\s*$//' file
# Comment out lines containing a specific pattern
sed -i '/sensitive_info/s/^/#/' file
# Chain multiple commands to first delete debug lines, then fix a typo
sed -i -e '/DEBUG/d' -e 's/eror/error/g' file
# Add a prefix to every line
sed 's/^/-> /' file
# Print a specific section of a file by patterns
sed -n '/start-section/,/end-section/p' file
# Delete all comments and blank lines
sed -e '/^#/d' -e '/^$/d' file
Add One Line To The Beginning
$ sed -i '1s/^/line to insert\n/' /path/to/file
Remove 1st line in place
$ sed -i 1d filename
Add comma to the end of the line
$ cat foo.txt | sed s/$/,/g
Remove https://
$ ... | sed s#https://##`