Skip to content
Tutorials Center
My learning

Topics

Lesson 7 of 38 ยท sed Basics

sed Addresses and Ranges

Tell sed which lines to act on: line numbers, regex addresses, ranges, first~step, addr,+N, the ! operator, nested blocks and the empty-regex shortcut.

A sed command without an address runs on every line. An address restricts it to some lines. Addresses are what turn s and d from blunt tools into precise ones: "delete lines 10 to 20", "in the section between [db] and the next blank line, change host", "every third line". This lesson covers every kind of address.

The shape of a command

TEXT
[address[,address]][!]command
  • no address: all lines
  • one address: lines that match it
  • two addresses: a range
  • ! after the address: the lines that do not match

Line number and last line

Bash Numbers and $

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Regex addresses

/regex/ selects lines that contain a match. Since the slash is the delimiter, escape slashes inside the regex or pick another delimiter with a backslash first: \|regex| (the \c form works for any c).

Bash Regex addresses

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

/^\/usr/s/bin/BIN/ reads: on lines starting with /usr, substitute. An address can be followed by any command, including s.

Ranges

A range A,B starts at the first line matching A and ends at the first line after it matching B, inclusive. The two ends can be line numbers, regexes or $.

Bash Ranges of several kinds

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Details worth knowing:

  • If the end is a line number smaller than the start line, only one line is matched (5,2p prints line 5).
  • After a range ends, sed starts looking for the start address again, so a range can match several times.
  • If the end regex never matches, the range runs to the end of the file.
  • The end regex is first tried on the line after the start. If you need the end test on the same line, use GNU's 0,/re/.

Steps and relative ranges (GNU)

GNU sed adds compact forms:

  • first~step: 1~2 is every odd line, 0~3 every third line.
  • addr,+N: the matching line and the N lines after it.
  • addr,~N: from the matching line up to the next line that is a multiple of N.
  • 0,/re/: from the beginning, ending at the first match (even when it is on line 1).
Bash GNU steps and relative ranges

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

The last example replaces only the first x even though the match is on line 1; a normal 1,/x/ would not stop at line 1 because it looks for the end from line 2.

Negation with !

! inverts the address: the command runs on every line that does not match. There may be spaces after it in GNU sed, but writing it tight (2!d) is portable.

Bash Everything except...

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Blocks

Braces { ... } group several commands under one address, separated by ; or newlines. They can be nested, which gives you "AND" logic: first the outer address, then the inner one.

Bash Blocks and nested addresses

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

The outer address /^\[web\]/,$ selects from [web] to the end; the inner /^host=/ narrows it further. The second command prints lines in the [db] range except the lines that start with [.

The empty regex

An empty regex // in an address or s//new/ means "the last regex that was used". It saves retyping long patterns:

Bash Reuse the last regex with //

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

/foo/ matches lines containing foo, and the empty pattern in s//FOO/ reuses it, so only foo is replaced.

Case-insensitive addresses (GNU)

Add I after the closing slash: /error/I matches Error, ERROR and error. For BSD sed use a bracket class.

Quick reference

Address Meaning
N line N
$ last line
/re/ lines matching re
\cREc same, delimiter c
N,M lines N to M
/a/,/b/ from a match of a to the next match of b
N,+K line N and K more (GNU)
F~S F, F+S, F+2S ... (GNU)
0,/re/ up to the first match, even on line 1 (GNU)
addr! lines not selected

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does sed -n '/start/,/end/p' print?

    A regex range includes both ends and can repeat if start appears again after an end.
  2. 2. What is sed '3!d' file?

    ! negates the address, so d runs on every line that is not line 3.
  3. 3. What does sed -n '0~3p' print (GNU sed)?

    first~step selects first, first+step, ...; with first 0 the lines are 3, 6, 9, and so on.
  4. 4. If the end address of a regex range never matches, what happens?

    Once started, the range stays active until the end address matches or the input ends.
  5. 5. How do you run s only on lines inside a block of braces?

    The address comes first, then braces enclose the commands it controls.

Summary

  • An address (line number, $, /re/, or a range) selects the lines a command runs on; ! inverts the selection.
  • Ranges are inclusive and can match many times; a missing end runs to the end of the file.
  • GNU adds first~step, addr,+N, addr,~N, 0,/re/ and the I modifier.
  • { } groups commands; blocks nest to combine conditions.
  • // reuses the last regex.

Next: Options and In-place Editing