Addressing Spans of Lines
Every sed command can be prefixed with an address that decides which lines it acts on. A single number like 5 targets exactly line 5, while a range written as addr1,addr2 targets every line from addr1 through addr2 inclusive. So sed '2,4d' deletes lines 2, 3, and 4. The special address $ means the very last line of the input, so 1,$ is every line and $d deletes only the final line. With no address at all, a command applies to every line in the stream.
Cricket analogy: A line range is like nominating overs 2 to 4 of a spell for review; the single $ address is the very last ball of the innings, the one that ends the match.
Regex-Bounded Ranges
Addresses need not be numbers: a /regex/ address selects every line the pattern matches, and you can combine two regexes into a range like /START/,/END/. This selects from the first line matching START through the next line matching END, inclusive. A subtle behaviour is that the range opens whenever addr1 matches and closes at the next addr2 match after it; if END never appears again, the range runs to the end of the file. You can even mix types, as in /BEGIN/,$ to select from a marker line to the end.
Cricket analogy: A regex range /START/,/END/ is like watching from the fall of the first wicket to the next drinks break; if drinks never come, you keep watching to stumps.
# Numeric range: delete lines 2 through 4
sed '2,4d' file.txt
# From line 10 to the end of file
sed -n '10,$p' file.txt
# Regex range: print everything between markers (inclusive)
sed -n '/BEGIN/,/END/p' config.txt
# Mixed: from the first match of ERROR to the last line
sed -n '/ERROR/,$p' log.txt
# GNU step notation: every 2nd line starting at line 1 (odd lines)
sed -n '1~2p' file.txt
# Every 3rd line starting at line 2
sed -n '2~3p' file.txtGNU Step Addressing
GNU sed adds a first~step address form for periodic selection. The address 1~2 matches lines 1, 3, 5, and so on (every second line starting at 1), while 0~3 matches every third line. This is invaluable for sampling or for operating on alternating rows. GNU also supports addr1,+N to mean 'addr1 and the next N lines' and addr1,~N to mean 'from addr1 through the next line whose number is a multiple of N'. These extensions are not part of POSIX, so they will not work on BSD or macOS sed.
Cricket analogy: The 1~2 step address is like watching only the first ball of every over; addr1,+2 is watching a wicket delivery plus the next two balls of that over.
The first~step form is a GNU extension. For POSIX-portable 'every Nth line', pipe through awk 'NR % N == 0' or use sed with a repeated pattern-space trick, since BSD/macOS sed rejects 1~2.
In a regex range /A/,/B/, addr2 is only tested on lines after addr1 matched. If A and B could match the SAME line, the range still spans at least two lines because B is not checked on the opening line itself.
- A single numeric address targets one line; addr1,addr2 targets an inclusive range of lines.
- $ is the special address for the last line of input; 1,$ means the whole file.
- A /regex/ address matches every line the pattern hits, and /A/,/B/ forms a range between markers.
- A regex range closes at the next addr2 match after addr1; if it never matches again, the range runs to end of file.
- GNU's first~step form (e.g. 1~2) selects every step-th line for periodic sampling.
- GNU also offers addr1,+N and addr1,~N, but these and first~step are not POSIX and fail on BSD/macOS sed.
Practice what you learned
1. What does sed '2,4d' do?
2. What does the $ address mean in sed?
3. In the range /BEGIN/,/END/, what happens if END never matches after BEGIN?
4. Which address selects every second line starting at line 1 in GNU sed?
5. Why might sed -n '1~2p' fail on macOS?
Was this page helpful?
You May Also Like
Negating Addresses
Using the ! operator to invert a sed address so a command runs on every line the address does NOT select, plus common idioms and gotchas.
The Print and Delete Commands
How sed's p (print) and d (delete) commands work, why -n changes everything, and the difference between d and D in the multi-line pattern space.
Regular Expressions in sed
How sed matches text using Basic and Extended Regular Expressions, including anchors, character classes, quantifiers, and the escaping rules that trip people up.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics