sed Address and address range Lines matching regular expression pattern


Example

$ cat ip.txt 
address
range
substitution
pattern
sample
Add Sub Mul Div
  • Lines matching a pattern

    $ sed '/add/d' ip.txt 
    range
    substitution
    pattern
    sample
    Add Sub Mul Div
    
    $ sed -n '/t/p' ip.txt 
    substitution
    pattern
    
    $ sed -n '/[A-Z]/ s| |/|gp' ip.txt 
    Add/Sub/Mul/Div
    
  • Range of patterns

    $ sed -n '/add/,/sub/p' ip.txt 
    address
    range
    substitution
    
    $ sed -n '/a/,/e/p' ip.txt 
    address
    range
    pattern
    sample
    

Note

  • In the second example, it matched two ranges - lines 1,2 and lines 4,5
  • See Using different delimiters on how to use other characters instead of / for specifying the pattern

GNU sed
  • Case-insensitive match

    $ sed -n '/add/Ip' ip.txt 
    address
    Add Sub Mul Div
    
    $ sed -n '/add/I,/sub/p' ip.txt 
    address
    range
    substitution
    Add Sub Mul Div