Range of selected lines the comma

To use sed effectively, we should be clear about how to define range. Range is typically two addresses in a file as follows:

  • Range with numbers:
'6d': range of line 6 
'3,6d': range from line 3 to 6 
  • Range with pattern:
'/pattern1/,/pattern2/ 
  • This will specify the range of all the lines between pattern1 and pattern2. We can even specify the range with a combination of both, that is, '/pattern/,6'. This will specify the range of lines between the pattern and line 6.

As mentioned, we can specify the range as numbers, pattern, or a combination of both, as shown here.

    $ cat country.txt
    Country     Capital           ISD Code
    USA         Washington  1
    China       Beijing           86
    Japan       Tokyo             81
    India       Delhi             91
    
    $ sed -n '/USA/,/Japan/p' country.txt ...

Get Learning Linux Shell Scripting - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.