sed Address and address range Specifying range using both number and pattern


Example

$ cat ip.txt 
address
range
substitution
pattern
sample
Add Sub Mul Div
  • Line number to line matching pattern

    $ sed -n '2,/pat/p' ip.txt 
    range
    substitution
    pattern
    
  • Line matching pattern to line number

    $ sed '/pat/,$d' ip.txt 
    address
    range
    substitution
    

GNU sed
  • Line matching pattern plus number of lines following it

    $ sed -n '/add/I,+1p' ip.txt 
    address
    range
    Add Sub Mul Div
    
  • 0 can be used as starting line number to signal end of range when pattern matches first line of input

    $ sed -n '0,/r/p' ip.txt 
    address
    
    $ sed -n '1,/r/p' ip.txt 
    address
    range
    
    $ sed -n '0,/u/p' ip.txt 
    address
    range
    substitution