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).
Last updated: August 2026

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 substitution s, deletion d, print p, append a, and insert i), and writing results to standard output, sed provides 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

  1. Read: Reads a line from the input stream and strips the trailing newline character.
  2. Pattern Space Load: Places the line into the temporary working memory called Pattern Space.
  3. Execute: Evaluates all sed instructions in order against the Pattern Space. If an address filter matches, the associated command runs.
  4. Output: Unless automatic printing is disabled (via the -n option), sed writes the contents of the Pattern Space to standard output, followed by a newline.
  5. Flush & Repeat: Clears the Pattern Space and repeats the cycle for the next line until End-Of-File (EOF).

Essential sed Command-Line Options

OptionLong OptionFunction & Description
-n--quiet, --silentSuppresses automatic printing of the pattern space at the end of each cycle. Used with p command.
-e <script>--expressionAdds commands to be executed; allows chaining multiple expressions on one command line.
-f <file>--fileReads sed commands and scripts from an external script file.
-i[SUFFIX]--in-placeEdits files in-place. If a suffix is provided (e.g., -i.bak), a backup copy is preserved.
-E / -r--regexp-extendedEnables Extended Regular Expressions (ERE), avoiding backslash escapes on (, ), {, }, +, ?, `

⚠️ LPIC-1 Trap: On GNU/Linux systems, sed -i 's/old/new/' file edits the file directly with no backup. If an argument is attached immediately to -i (e.g., sed -i.orig 's/old/new/' file), sed saves the unmodified original as file.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 FormatMeaningPractical Example
N (Line Number)Matches specifically line number Nsed '5d' file (deletes line 5)
$ (Last Line)Matches the final line of the input streamsed '$d' file (deletes the last line)
N,M (Line Range)Matches from line N through line M inclusivesed -n '10,20p' file (prints lines 10 to 20)
N,$ (To End)Matches from line N to the end of the filesed '5,$d' file (keeps only first 4 lines)
first~step (Step)Matches every step-th line starting at firstsed -n '1~2p' file (prints all odd lines: 1, 3, 5...)
/pattern/ (Regex)Matches lines matching the regular expressionsed -n '/ERROR/p' /var/log/syslog
/start/,/end/Matches from line matching start to line matching endsed -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. sed ’[address]s/pattern/replacement/flags’ [file]\text{sed '[address]s/pattern/replacement/flags' [file]}

Substitution Flags

  • g (Global): Replaces all non-overlapping occurrences of the pattern on the line (without g, 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/2 replaces only the second foo).
  • 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

CommandOperation NameDescription & Example
dDeleteDeletes the Pattern Space immediately and begins next cycle: sed '/DEBUG/d' app.log
pPrintPrints the current Pattern Space to stdout: sed -n '1,5p' /etc/passwd
aAppendAppends text on a new line after the addressed line: sed '2a\\\\New line content' file
iInsertInserts text on a new line before the addressed line: sed '1i\\\\# Script Header' file
cChangeReplaces the entire addressed line(s) with new text: sed '/SERVER_NAME/c\\\\SERVER_NAME=prod01' config
yTransliterateTranslates characters one-to-one (identical to tr): sed 'y/abc/ABC/' file
qQuitTerminates 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 sed option that suppresses the automatic printing of pattern space so that only lines explicitly matching a p command are displayed, the answer is -n (or --quiet / --silent). When asked for the option to edit files directly in place, the answer is -i.

Test Your Knowledge

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
B
C
D
Test Your Knowledge

A system administrator executes the command sed -n '15,25p' server.log. What is the exact behavior of this command?

A
B
C
D
Test Your Knowledge

What is the result of executing sed '/^#/d; /^$/d' config.txt on a system configuration file?

A
B
C
D