5.5 Stream Editing with sed (103.2)
Key Takeaways
- sed is a non-interactive stream editor operating line-by-line via Pattern Space (working buffer) and Hold Space (storage buffer).
- Addressing models support line numbers (1, $), step intervals (1~2), regex patterns (/pattern/), ranges (/start/,/end/), and negation (!).
- The substitution command s/regex/replacement/flags supports global replacement (g), case insensitivity (i), print (p), and custom delimiters (s#pattern#repl#).
- Backreferences (\1, \2) and the whole-match token (&) allow dynamic reconstruction of matched patterns.
- Key sed operational flags include -n (suppress auto-print), -i (in-place file editing), and -E / -r (extended regular expressions).
5.5 Stream Editing with sed (103.2)
Quick Summary: The GNU
sed(Stream Editor) utility performs automated, non-interactive text transformations on input streams or files. By reading input line-by-line into its internal Pattern Space, applying specified addressing rules and commands (such as substitutions, deletiond, printp, appenda, and inserti), and writing results to standard output,sedprovides powerful batch editing capabilities essential for Topic 103.2.
1. sed Architecture & Execution Model
Unlike interactive text editors (vi or nano), sed is a stream editor that processes data sequentially without user interaction.
Internal sed Execution Cycle for Each Input Line:
┌─────────────────────────────────────────────────────────────────────────┐
│ Input Stream (File or Stdin) │
└────────────────────────────────────┬────────────────────────────────────┘
│ 1. Read next line & strip trailing \\n
▼
┌─────────────────────────────────────────────────────────────────────────┐
│ PATTERN SPACE (Primary Working Buffer in RAM) │
│ • Current line resides here during processing │
│ • sed commands execute sequentially on this buffer │
│ (e.g., s/foo/bar/, /error/d, 10p) │
└───────────────────▲────────────────┬───────────────────▲────────────────┘
h / H (Copy/ │ │ g / G (Copy/ │ x (Exchange)
Append to Hold)│ │ Append from Hold) │
▼ │ ▼
┌────────────────────────────────────┼────────────────────────────────────┐
│ HOLD SPACE │ │
│ (Secondary Persistent Scratchpad) │ │
└────────────────────────────────────┘ │
│ 2. Automatic Output (Unless -n set)│
▼ │
┌─────────────────────────────────────────────────────────────────────────┴┐
│ Standard Output (stdout) │
└─────────────────────────────────────────────────────────────────────────┘
The Standard sed Processing Cycle
- Read: Reads a line from the input stream and strips the trailing newline character.
- Pattern Space Load: Places the line into the temporary working memory called Pattern Space.
- Execute: Evaluates all sed instructions in order against the Pattern Space. If an address filter matches, the associated command runs.
- Output: Unless automatic printing is disabled (via the
-noption),sedwrites the contents of the Pattern Space to standard output, followed by a newline. - Flush & Repeat: Clears the Pattern Space and repeats the cycle for the next line until End-Of-File (EOF).
Essential sed Command-Line Options
| Option | Long Option | Function & Description |
|---|---|---|
-n | --quiet, --silent | Suppresses automatic printing of the pattern space at the end of each cycle. Used with p command. |
-e <script> | --expression | Adds commands to be executed; allows chaining multiple expressions on one command line. |
-f <file> | --file | Reads sed commands and scripts from an external script file. |
-i[SUFFIX] | --in-place | Edits files in-place. If a suffix is provided (e.g., -i.bak), a backup copy is preserved. |
-E / -r | --regexp-extended | Enables Extended Regular Expressions (ERE), avoiding backslash escapes on (, ), {, }, +, ?, ` |
⚠️ LPIC-1 Trap: On GNU/Linux systems,
sed -i 's/old/new/' fileedits the file directly with no backup. If an argument is attached immediately to-i(e.g.,sed -i.orig 's/old/new/' file),sedsaves the unmodified original asfile.orig.
2. Addressing Mechanisms in sed
By default, sed commands apply globally to every line of input. Addresses restrict command execution to specific lines, ranges, or regular expression matches.
Addressing Paradigms
| Addressing Format | Meaning | Practical Example |
|---|---|---|
N (Line Number) | Matches specifically line number N | sed '5d' file (deletes line 5) |
$ (Last Line) | Matches the final line of the input stream | sed '$d' file (deletes the last line) |
N,M (Line Range) | Matches from line N through line M inclusive | sed -n '10,20p' file (prints lines 10 to 20) |
N,$ (To End) | Matches from line N to the end of the file | sed '5,$d' file (keeps only first 4 lines) |
first~step (Step) | Matches every step-th line starting at first | sed -n '1~2p' file (prints all odd lines: 1, 3, 5...) |
/pattern/ (Regex) | Matches lines matching the regular expression | sed -n '/ERROR/p' /var/log/syslog |
/start/,/end/ | Matches from line matching start to line matching end | sed -n '/BEGIN CERT/,/END CERT/p' cert.pem |
addr! (Negation) | Inverts address matching (applies to lines not matching address) | sed '/^#/!d' config.conf (deletes all non-comment lines) |
# Deleting empty lines and lines starting with '#' comments:
$ sed -e '/^#/d' -e '/^[[:space:]]*$/d' /etc/nginx/nginx.conf
# Printing only the block between '<VirtualHost>' and '</VirtualHost>':
$ sed -n '/<VirtualHost>/,/</VirtualHost>/p' /etc/httpd/conf/httpd.conf
3. The Substitution Command (s)
The substitution command is the most frequently tested sed operation on LPIC-1.
Substitution Flags
g(Global): Replaces all non-overlapping occurrences of the pattern on the line (withoutg, only the first occurrence on each line is replaced).p(Print): Prints the Pattern Space if a successful substitution occurred (almost always paired with-n).i/I(Ignore Case): Case-insensitive regular expression matching.N(Numeric Instance): Replaces only the N-th occurrence of the pattern on the line (e.g.,s/foo/bar/2replaces only the secondfoo).w <file>(Write): Writes the modified line directly to the specified file.
Custom Delimiters (Preventing Leaning Toothpick Syndrome)
While the forward slash (/) is standard, sed allows any single character to serve as the delimiter for the s command. This is critical when manipulating file paths or URLs:
# Standard slash delimiter requires ugly escaping:
$ sed 's/\\/var\\/www\\/html/\\/srv\\/www\\/public/g' config.txt
# Using '#' or '|' as custom delimiters:
$ sed 's#/var/www/html#/srv/www/public#g' config.txt
$ sed 's|https://example.com|https://secure.example.com|g' links.txt
Backreferences (\\1–\\9) and Matched Pattern (&)
&: Represents the entire string that matched the search regular expression.\\1,\\2, ...\\9: Represents sub-expressions captured inside parentheses (\\( ... \\)in Basic Regular Expressions or( ... )with-E/-r).
# Wrapping all numbers in square brackets using '&':
$ echo "Port 8080 and Port 443" | sed 's/[0-9]\\+/[&]/g'
Port [8080] and Port [443]
# Swapping two words using capture groups and backreferences:
$ echo "Doe, John" | sed -E 's/([A-Za-z]+), ([A-Za-z]+)/\\2 \\1/'
John Doe
# Extracting IP addresses from ifconfig output:
$ ip -4 addr show eth0 | sed -n -E 's/.*inet ([0-9.]+).*/\\1/p'
192.168.1.50
4. Other Core sed Commands
| Command | Operation Name | Description & Example |
|---|---|---|
d | Delete | Deletes the Pattern Space immediately and begins next cycle: sed '/DEBUG/d' app.log |
p | Prints the current Pattern Space to stdout: sed -n '1,5p' /etc/passwd | |
a | Append | Appends text on a new line after the addressed line: sed '2a\\\\New line content' file |
i | Insert | Inserts text on a new line before the addressed line: sed '1i\\\\# Script Header' file |
c | Change | Replaces the entire addressed line(s) with new text: sed '/SERVER_NAME/c\\\\SERVER_NAME=prod01' config |
y | Transliterate | Translates characters one-to-one (identical to tr): sed 'y/abc/ABC/' file |
q | Quit | Terminates sed execution immediately without processing further lines: sed '10q' huge_file.log |
# Using 'q' (Quit) as a high-performance alternative to head on multi-gigabyte files:
# Unlike 'head', 'sed 5q' exits immediately after line 5 without reading the remainder of the stream:
$ sed '5q' /var/log/messages
💡 LPIC-1 Exam Fill-in-the-Blank Alert: When asked for the
sedoption that suppresses the automatic printing of pattern space so that only lines explicitly matching apcommand are displayed, the answer is-n(or--quiet/--silent). When asked for the option to edit files directly in place, the answer is-i.
Which sed command will replace ALL occurrences of the string 'http://' with 'https://' across every line in the file web.conf and modify the file directly in place without writing to standard output?
A system administrator executes the command sed -n '15,25p' server.log. What is the exact behavior of this command?
What is the result of executing sed '/^#/d; /^$/d' config.txt on a system configuration file?