Skip to Content
Learning Linux Shell Scripting - Second Edition
book

Learning Linux Shell Scripting - Second Edition

by Ganesh Sanjiv Naik
May 2018
Beginner
332 pages
7h 28m
English
Packt Publishing
Content preview from Learning Linux Shell Scripting - Second Edition

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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learning Linux Shell Scripting

Learning Linux Shell Scripting

Ganesh Sanjiv Naik
Linux Shell Scripting Cookbook - Third Edition

Linux Shell Scripting Cookbook - Third Edition

Clif Flynt, Sarath Lakshman, Shantanu Tushar

Publisher Resources

ISBN: 9781788993197Supplemental Content