Understanding sed Address Patterns and Command Options

The stream editor processes input sequentially, applying transformation rules within a temporary pattern space before flushing results to standard output. Unlike interactive editors, it operates without loading entire files into memory, making it highly efficient for largee datasets and pipeline operations.

Core Command Flags

Configuration flags dictate how sed interacts with input streams, manages output verbosity, and evlauates regular expressions.

By default, sed echoes every processed line to standard output. Appending a print directive duplicates the targeted entries:

$ cat access.log
$ sed '4p' access.log

The -n flag suppresses automatic echoing, restricting output to lines explicitly matched by print commands:

$ sed -n '4p' access.log
$ sed -n '/ERROR/p' access.log

Complex instruction sets can be externalized using the -f flag, wich reads commands from a dedicated script file:

$ cat transforms.sc
/^WARNING/p
/CRITICAL/p
$ sed -n -f transforms.sc access.log

Enable modern regular expression syntax (alternation, grouping, non-greedy matching) with -r:

$ sed -n -r '/(timeout|refused)/p' access.log

Text substitution operates independently of print flags. Without -n, unmodified lines still appear. Combining substitution with conditional printing isolates only altered records:

$ sed -n 's/v1/v2/g;p' access.log

The -i flag bypasses standard output entirely, writing modifications directly back to the source file:

$ sed -i 's/temp_dir/production_dir/g' access.log

Addressing and Range Syntax

Address patterns determine exactly which lines or blocks receive commands. sed evaluates these patterns in a deterministic sequence before applying actions.

Numeric Addressing

Target a single absolute line position:

$ sed -n '22p' records.csv

Define a closed interval using start and end line numbers:

$ sed -n '8,16p' records.csv

Specify a starting line followed by a relative offset to capture a dynamic block:

$ sed -n '5,+4p' records.csv

Regex-Based Addressing

Filter lines matching a specific prefix pattern:

$ sed -n '/^daemon/p' /etc/passwd

Extract contiguous blocks bounded by two independent regex matches:

$ sed -n '/^bin/,/^sys/p' /etc/passwd

Initiate extraction from a fixed line number and terminate when a regex condition is met:

$ sed -n '3,/^sshd/p' /etc/passwd

Begin matching at a pattern occurrence and stop at a predetermined line number:

$ sed -n '/daemon/,14p' /etc/passwd

Practical Matching Workflows

Retrieve a target line plus the subsequent three lines:

$ sed -n '7,+3p' /etc/passwd

Isolate entries containing a specific executable path:

$ sed -n '/bin/bash/p' /etc/passwd

Search for strings requiring delimiter escaping due to forward slashes:

$ sed -n '/\/var\/log\/syslog/p' /etc/passwd

Match a starting pattern boundary and end at a different pattern boundary:

$ sed -n '/^nobody/,/^root/p' /etc/passwd

Combine a regex start condition with an absolute line termination point:

$ sed -n '/nobody/,11p' /etc/passwd

Tags: Linux sed stream-editor regular-expressions unix-utilities

Posted on Fri, 03 Jul 2026 16:51:26 +0000 by drew69