Mastering sed: Essential Commands, Options, and Real‑World Examples
This comprehensive guide explains how the stream editor sed works, covering its syntax, common options, matching patterns, delimiters, variable handling, core editing commands, advanced features, typical use‑case examples, script files, hold/ pattern spaces, and techniques for printing specific lines or exiting early.
What is sed?
sed (stream editor) reads input line by line, stores each line in a temporary buffer called the pattern space , applies editing commands to lines that match a given address, and prints the result. By default the original file is not modified; in‑place editing requires the -i option.
Basic syntax
sed [OPTION]... 'script' [input-file]...Common options
-n, --quiet, --silent: suppress automatic printing of the pattern space (useful with p). --debug: annotate program execution for debugging. -e script, --expression=script: specify a script on the command line; multiple -e arguments are allowed. -f script-file, --file=script-file: read commands from a file. -i[SUFFIX], --in-place[=SUFFIX]: edit files in place; if SUFFIX is given a backup copy is created. -l N, --line-length=N: set line‑wrap length for the l command. --posix: disable GNU extensions and follow POSIX syntax. -E, -r, --regexp-extended: enable extended regular expressions (ERE). -s, --separate: treat each input file as a separate stream. --sandbox: run in sandbox mode, disabling e, r, w commands. -u, --unbuffered: reduce buffering and flush output more often. -z, --null-data: use NUL ( \0) as line delimiter (useful for binary data).
Addressing (matching conditions)
/regexp/: regular‑expression match. n (line number): match a specific line; ranges can be expressed as 1-$ (first to last line). addr1,addr2: match a range from addr1 to addr2 (both may be line numbers or regexes). addr1,+n: match addr1 and the following n lines. n~step: match every step ‑th line starting at line n (e.g., 1~2 for odd lines).
Delimiters
Any character can be used as a delimiter in a s command. When the chosen delimiter appears inside the pattern it must be escaped.
sed 's\|foo\|bar\g' file.txt
sed 's:foo:bar:g' file.txt
sed 's/\/bin/\/usr\/bin/g' file.txtVariable references
Use double quotes when the sed script contains shell variables so that the variable is expanded before sed sees the script.
foo="world"
echo "hello $foo" | sed "s/$foo/librarookie/"
# Output: hello librarookieCore editing commands
! addr!command: apply command to lines that do not match addr.
p addr p: print the current line (often used with -n).
l addr l: print the line showing non‑printing characters.
= addr =: print the current line number.
a\<text> addr a\text: append text after the matched line.
i\<text> addr i\text: insert text before the matched line.
c\<text> addr c\text: replace the entire matched line with text.
d addr d: delete the matched line.
r <filename> addr r file: read the contents of file and insert after the matched line.
w <filename> addr w file: write the matched line to file.
q [exit‑code] addr q: quit processing immediately (optional exit code).
s/regexp/replace/[flags] : substitute the first (or all with g) occurrence of regexp with replace. Flags: g: global replacement. n (numeric): replace only the n ‑th occurrence on the line. i: case‑insensitive match.
Advanced commands
h: copy pattern space to hold space. H: append pattern space to hold space. g: replace pattern space with hold space. G: append hold space to pattern space. x: exchange pattern space and hold space. n: read the next line into pattern space (replaces current line). N: read the next line and append it to pattern space (separated by a newline). :label, t label, b label: define a label and branch to it conditionally ( t) or unconditionally ( b). y/old/new/: transliterate characters (like tr).
Typical use‑case examples
Range selection
# Print lines 3 to 5
sed -n '3,5p' file.txt
# Print from a start pattern to an end pattern
sed -n '/start/,/end/p' file.txt
# Print until the first blank line
sed -n '1,/^$/p' file.txt
# Print odd‑numbered lines
sed -n '1~2p' file.txtSubstitution (replace) examples
Replace the first occurrence on each line: sed 's/foo/bar/' file.txt Replace only the third match on each line: sed 's/foo/bar/3' file.txt Global replace (all occurrences): sed 's/foo/bar/g' file.txt Case‑insensitive replace: sed 's/foo/bar/i' file.txt Write replaced lines to a new file:
sed 's/foo/bar/w output.txt' file.txtInsert, append, change
# Append after lines containing "hello"
sed -i '/hello/a\this is a test line' file.txt
# Insert before lines containing "librarookie"
sed -i '/librarookie/i\this is a test line' file.txt
# Change an entire line containing "librarookie"
sed -i '/librarookie/c\this is a test line' file.txtDelete operations
# Delete the whole file content (dangerous)
sed -i 'd' file.txt
# Delete line 2
sed -i '2d' file.txt
# Delete blank lines
sed -i '/^$/d' file.txt
# Delete comment lines starting with #
sed -i '/^#/d' file.txtScript files ( -f )
A sed script file contains one command per line; comments start with #. Example script.sed:
# Replace "old_string" on the first line
1s/old_string/new_string/
# Replace "foo" with "bar" from line 3 to 5
3,5s/foo/bar/g
# Delete lines containing "delete_this_line"
/delete_this_line/d
# Insert a line before lines matching "insert_before"
/insert_before/i\
This is the new line to insert
# Append a line at the end of the file
$ a\
This is the new line at the end of the fileRun with sed -f script.sed file.txt. Adding -n prevents automatic printing.
Hold space and pattern space
The hold space allows temporary storage between cycles. Example: duplicate lines containing test at the end of the file. sed -e '/test/h' -e '$G' file.txt Exchange hold and pattern spaces:
sed -e '/test/h' -e '/check/x' file.txtPrinting odd/even lines
# Odd lines using n
sed -n 'p;n' test.txt
# Even lines using n
sed -n 'n;p' test.txt
# Odd lines using step
sed -n '1~2p' test.txt
# Even lines using step
sed -n '2~2p' test.txtQuit command
# Stop processing after line 10
sed '10q' file.txtSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Raymond Ops
Linux ops automation, cloud-native, Kubernetes, SRE, DevOps, Python, Golang and related tech discussions.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
