Mastering sed: Essential Commands, Options, and Real‑World Examples
This comprehensive guide explains how the sed stream editor works, covering its syntax, common options, delimiters, variable references, core editing commands, advanced operations, script usage, and the concepts of pattern and hold spaces, all illustrated with practical examples.
Introduction to sed
sed is a stream editor that efficiently performs replacements, deletions, insertions, and other operations on text data line by line without modifying the original file.
It reads input data into a temporary buffer called the pattern space , matches it against given conditions, and executes specific sed commands when a match succeeds.
Command Syntax
sed [OPTION]... {script-only-if-no-other-script} [input-file]...Examples:
cat filename | sed 's/pattern/replacement/'Common Options
-n, --quiet, --silent: suppress automatic printing of pattern space (often used with p). --debug: annotate program execution for debugging. -e script, --expression=script: add a script directly on the command line (multiple expressions allowed). -f script-file, --file=script-file: read commands from a script file. -i[SUFFIX], --in-place[=SUFFIX]: edit files in place, optionally creating a backup. -E, -r, --regexp-extended: use extended regular expressions. --posix: disable GNU extensions and follow POSIX syntax.
Delimiters
While / is the default delimiter, any character can be used. When a delimiter appears inside the expression, it must be escaped.
sed 's\|foo\|bar\|g' filenameVariable References
sed scripts are usually single‑quoted. If the script contains variable names, use double quotes.
foo="world"
echo "hello $foo" | sed "s/$foo/librarookie/"Editing Commands
p: print the current line (often with -n). l: print the line showing control characters. =: print the current line number. a\text: append text after the matched line. i\text: insert text before the matched line. c\text: replace the entire matched line with text . d: delete the matched line. r filename: read filename and insert its contents after the matched line. w filename: write the matched line to filename . s/regexp/replace/: substitute the first match on a line. s/regexp/replace/g: global substitution on a line. q: quit processing immediately.
Advanced Commands
h: copy pattern space to hold space. H: append pattern space to hold space. g: copy hold space back to pattern space. G: append hold space to pattern space. x: exchange pattern space and hold space. n: read the next line into pattern space. N: read the next line and append it to pattern space. t label / b label: conditional or unconditional jumps to a label. y/source/dest/: transliterate characters.
Combining Multiple Expressions
Use -e to specify several expressions, separate them with semicolons, or pipe multiple sed commands.
sed -e 's/foo/bar/' -e '/baz/d' filenameScripts (-f)
A sed script file contains a list of commands, one per line. Blank lines and comments (starting with #) are ignored.
# Replace "old_string" with "new_string" on the first line
1s/old_string/new_string/
# Delete lines containing "delete_this_line"
/delete_this_line/dPattern Space and Hold Space
The pattern space holds the current line being processed. The hold space is a secondary buffer that can store data for later use, enabling complex multi‑line operations.
# Copy lines containing "test" to hold space and append at end of file
sed -e '/test/h' -e '$G' filePrinting Odd or Even Lines
# Odd lines
sed -n 'p;n' file
# Even lines
sed -n 'n;p' file
# Using step syntax
sed -n '1~2p' file # odd
sed -n '2~2p' file # evenQuit After a Certain Line
# Stop after printing line 10
sed '10q' filenameSigned-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.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
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.
