Skip to content

Sed Pattern Range

In sed, commands can be restricted to operate only on lines that match a specific address or a range of addresses. An address can be a line number, a pattern (regular expression), or a combination. This chapter focuses on using patterns to define these ranges. Remember, by default, sed prints every line after processing. The -n option suppresses this default output, and the p command explicitly prints lines.

You can specify a single pattern. sed will apply the command to any line that matches this pattern. A pattern is enclosed in slashes (/pattern/).

Example: Print lines containing the string “Paulo” from books.txt. Assume books.txt contains lines like: 3) The Alchemist, Paulo Coelho, 197 5) The Pilgrimage, Paulo Coelho, 288

$ sed -n '/Paulo/ p' books.txt

Output:

3) The Alchemist, Paulo Coelho, 197
5) The Pilgrimage, Paulo Coelho, 288

Here, sed processes each line of books.txt. If a line contains “Paulo”, the p command is executed for that line, printing it.

You can combine a pattern with a line number to define a range. The range /pattern/,lineNumber starts with the first line matching pattern and continues up to (and including) lineNumber.

Example: Print lines starting from the first line containing “Alchemist” up to line 5.

$ sed -n '/Alchemist/,5 p' books.txt

Assuming books.txt has relevant content, the output might be:

3) The Alchemist, Paulo Coelho, 197
4) The Fellowship of the Ring, J. R. R. Tolkien, 432
5) The Pilgrimage, Paulo Coelho, 288

You can use the $ character, which represents the last line of the input, to define a range from a pattern match to the end of the file.

Example: Print all lines from the first occurrence of “The” to the end of books.txt.

$ sed -n '/The/,$ p' books.txt

Output (example):

2) The Two Towers, J. R. R. Tolkien, 352
3) The Alchemist, Paulo Coelho, 197
4) The Fellowship of the Ring, J. R. R. Tolkien, 432
5) The Pilgrimage, Paulo Coelho, 288
6) A Game of Thrones, George R. R. Martin, 864

You can specify a range defined by two patterns: /pattern1/,/pattern2/. sed will start applying the command on the first line matching pattern1 and continue until a line matching pattern2 is found. If pattern2 is not found after pattern1, it continues to the end of the file.

Example: Print all lines starting from one containing “Two” up to a line containing “Pilgrimage”.

$ sed -n '/Two/,/Pilgrimage/ p' books.txt

Output (example):

2) The Two Towers, J. R. R. Tolkien, 352
3) The Alchemist, Paulo Coelho, 197
4) The Fellowship of the Ring, J. R. R. Tolkien, 432
5) The Pilgrimage, Paulo Coelho, 288

Pattern and Relative Line Count (GNU sed extension)

Section titled “Pattern and Relative Line Count (GNU sed extension)”

GNU sed allows specifying a range from a pattern match plus a certain number of subsequent lines using the syntax /pattern/,+N. This means the line matching pattern and the next N lines.

Example: Find the first line containing “Two” and print that line plus the next 4 lines.

$ sed -n '/Two/,+4 p' books.txt

Output (example):

2) The Two Towers, J. R. R. Tolkien, 352
3) The Alchemist, Paulo Coelho, 197
4) The Fellowship of the Ring, J. R. R. Tolkien, 432
5) The Pilgrimage, Paulo Coelho, 288
6) A Game of Thrones, George R. R. Martin, 864

Note on Regular Expressions: The patterns used in these examples are simple strings. However, sed patterns are actually basic regular expressions by default, allowing for much more sophisticated matching. For example, /^Start/ matches “Start” at the beginning of a line. You can enable extended regular expressions with the -E (or -r in GNU sed) option for more complex patterns.

Experimentation is key to mastering sed’s addressing capabilities. Try these examples with your own text files and modify the patterns and ranges to observe different outcomes.