100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Programming

Line Ranges and Steps

Selecting spans of lines in sed using numeric ranges, the $ last-line symbol, regex-bounded ranges, and GNU's first~step notation for periodic selection.

Patterns & AddressingBeginner9 min readJul 10, 2026
Analogies

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.

bash
# 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.txt

GNU 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

Was this page helpful?

Topics covered

#Programming#SedStreamEditorStudyNotes#LineRangesAndSteps#Line#Ranges#Steps#Addressing#StudyNotes#SkillVeris#ExamPrep