Sed Pattern Range
Sed - Addressing with Patterns and Ranges
Section titled “Sed - Addressing with Patterns and Ranges”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.
Single Pattern Address
Section titled “Single Pattern Address”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.txtOutput:
3) The Alchemist, Paulo Coelho, 1975) The Pilgrimage, Paulo Coelho, 288Here, sed processes each line of books.txt. If a line contains “Paulo”, the p command is executed for that line, printing it.
Pattern and Line Number Range
Section titled “Pattern and Line Number Range”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.txtAssuming books.txt has relevant content, the output might be:
3) The Alchemist, Paulo Coelho, 1974) The Fellowship of the Ring, J. R. R. Tolkien, 4325) The Pilgrimage, Paulo Coelho, 288Pattern to End-of-File Range
Section titled “Pattern to End-of-File Range”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.txtOutput (example):
2) The Two Towers, J. R. R. Tolkien, 3523) The Alchemist, Paulo Coelho, 1974) The Fellowship of the Ring, J. R. R. Tolkien, 4325) The Pilgrimage, Paulo Coelho, 2886) A Game of Thrones, George R. R. Martin, 864Range Between Two Patterns
Section titled “Range Between Two Patterns”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.txtOutput (example):
2) The Two Towers, J. R. R. Tolkien, 3523) The Alchemist, Paulo Coelho, 1974) The Fellowship of the Ring, J. R. R. Tolkien, 4325) The Pilgrimage, Paulo Coelho, 288Pattern 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.txtOutput (example):
2) The Two Towers, J. R. R. Tolkien, 3523) The Alchemist, Paulo Coelho, 1974) The Fellowship of the Ring, J. R. R. Tolkien, 4325) The Pilgrimage, Paulo Coelho, 2886) A Game of Thrones, George R. R. Martin, 864Note 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.