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:
  • 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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.