Mastering sed: Stream Editing Basics, Commands, and Advanced Tricks
This article explains the fundamentals of the sed stream editor, covering its execution model, common parameters and actions, addressing methods, pattern and hold spaces, label usage, and provides multiple practical examples illustrating typical command patterns and advanced techniques.
sed Stream Editor Overview
Execution principle
Parameters and actions
Addressing
Pattern space and hold space
Labels
What is sed? sed is a stream editor commonly used as a filter in shell scripts and Makefiles. It reads input from the previous program, applies editing commands, and outputs the transformed text. The basic command format is sed /pattern/action.
sed does not modify the original file unless the output is redirected. It supports basic regular expressions; special characters such as ( ) { } | must be escaped unless using extended mode.
Parameters and actions
Common options include: -n: suppress automatic printing; only lines explicitly printed are output. -e: allow multiple editing commands. -i: edit files in place. -f: specify a script file.
Typical actions: a: append text after the current line. c: replace the entire line with new text. i: insert text before the current line. p: print the selected line. s: substitute a pattern with another string; s/pattern1/pattern2/ replaces the first occurrence, s/pattern1/pattern2/g replaces all occurrences.
Addressing
Addressing determines which lines are edited. Addresses can be line numbers, regular expressions, or ranges. Examples:
sed '/start/,/end/d' file # delete lines from start to end inclusive
sed '/start/,10d' file # delete from start line to line 10Pattern space and hold space
The pattern space holds the current line for processing; the hold space is a temporary storage area. Typical commands: g: copy hold space to pattern space. G: append hold space to pattern space. h: copy pattern space to hold space. H: append pattern space to hold space. d: delete pattern space and read next line. D: delete first line of pattern space without reading next line. n: print pattern space, read next line, and continue. N: read next line and append to pattern space. x: exchange pattern and hold spaces.
Using labels
Labels enable branching within scripts: :a defines label a. ba jumps to label a. $ matches the last line. ! prevents further processing.
Example: $!ba means “for all lines except the last, jump to label a”.
Examples
Various practical examples illustrate common tasks such as adding blank lines, simulating reverse output, appending matched lines, converting columns to a single line, summing numbers, and printing odd/even lines. (Images omitted for brevity.)
sed also supports regular expression metacharacters similar to grep, with the pattern enclosed in slashes. The delimiter can be changed, e.g., sed -n '\o^56op' datafile. Common regex symbols include ^, $, ., *, [], [^], &, \<, \>, and quantifiers like \{m\}, \{m,\}, \{m,n\}.
Signed-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.
