Study guide

5.5 Stream Editing with sed (103.2)

LPIC-1 Linux Administrator — Exam 101 101-500

Track your progress as you read

Finished sections get a check mark.

1 Exam Overview & Study Strategy

1.1 LPIC-1 101-500 Exam Structure & Blueprint 1.2 Command Line & Shell Fundamentals for Exam Success 1.3 Fill-in-the-Blank Strategies & Common Traps

2 Topic 101: System Architecture

2.1 Hardware Discovery, Sysfs, Procfs & Kernel Modules (101.1) 2.2 Udev, Dbus & Device Management (101.1) 2.3 BIOS vs. UEFI & System Boot Process (101.2) 2.4 Kernel Initialization, Initramfs & System Logs (101.2) 2.5 Systemd Boot Targets & Unit Management (101.3) 2.6 SysVinit Runlevels, Telinit & System Shutdown (101.3)

3 Topic 102: Linux Installation & Boot Management

3.1 Hard Disk Layout, Partition Schemes & Swap Design (102.1) 3.2 LVM & Standard Mount Point Allocation (102.1) 3.3 GRUB 2 Configuration & Troubleshooting (102.2) 3.4 Legacy GRUB & Alternative Boot Loaders (102.2) 3.5 Managing Shared Libraries & ldconfig (102.3) 3.6 Linux as a Virtualization Guest & Cloud Instances (102.6)

4 Topic 102: Package Management Systems

4.1 Debian Low-Level Package Management with dpkg (102.4) 4.2 High-Level Debian Package Management: apt, apt-get & apt-cache (102.4) 4.3 RPM Package Management with rpm (102.5) 4.4 YUM, DNF & Zypper Package Management (102.5) 4.5 Package Repositories & Integrity Verification (102.4/102.5) 4.6 Universal Linux Packages: Snap, Flatpak & AppImage (102.4/102.5)

5 Topic 103: Command Line & Text Processing

5.1 Shell Environment, Environment Variables & Aliases (103.1) 5.2 Command History, Quoting & Expansion (103.1) 5.3 Basic Text Filters: cat, head, tail, cut, paste, nl, od (103.2) 5.4 Advanced Text Filters: sort, uniq, wc, tr, split, less & Checksums (103.2) 5.5 Stream Editing with sed (103.2) 5.6 Basic File Management: cp, mv, rm, touch, mkdir, rmdir (103.3) 5.7 Archiving and Compression: tar, gzip, bzip2, xz, cpio & dd (103.3)

6 Topic 103: Streams, Processes, Regex & Vi

6.1 Standard Streams, Redirection & File Descriptors (103.4) 6.2 Pipelines, tee & xargs (103.4) 6.3 Process Monitoring: ps, top, free, watch, pgrep, pstree & uptime (103.5) 6.4 Signals, kill, Job Control, nohup, screen & tmux (103.5) 6.5 Process Priorities: nice & renice (103.6) 6.6 Regular Expressions & grep, egrep, fgrep (103.7) 6.7 vi / vim Modes, plus nano, Emacs & EDITOR Awareness (103.8)

7 Topic 104: Devices, Partitions & Filesystems

7.1 Block Devices, fdisk & gdisk (104.1) 7.2 GNU Parted & Partition Table Management (104.1) 7.3 Creating Filesystems: mkfs, ext4, XFS, VFAT/exFAT & Btrfs (104.1) 7.4 Filesystem Integrity: fsck, e2fsck, xfs_repair, xfs_fsr & xfs_db (104.2) 7.5 Monitoring Disk Usage: df, du & tune2fs (104.2) 7.6 Managing Swap Space: mkswap, swapon & swapoff (104.1/104.2)

8 Topic 104: Mounts, Permissions, Links & FHS

8.1 Mounting and Unmounting Filesystems: mount & umount (104.3) 8.2 Persistent Mounts: /etc/fstab, UUIDs & systemd mount units (104.3) 8.3 Standard File Permissions: chmod, chown, chgrp & umask (104.5) 8.4 Special Permissions: SUID, SGID & Sticky Bit (104.5) 8.5 Hard Links vs. Symbolic Links: ln & Inodes (104.6) 8.6 Filesystem Hierarchy Standard (FHS) & File Location (104.7)

5.5 Stream Editing with sed (103.2)

Key Takeaways Explain This Topic AI

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

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 -n option), sed writes 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/' 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 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. sed ’[address]s/pattern/replacement/flags’ [file] \text{sed '[address]s/pattern/replacement/flags' [file]} 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

  • Command Operation Name Description & Example
  • d Delete Deletes the Pattern Space immediately and begins next cycle: sed '/DEBUG/d' app.log
  • p Print 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 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 .