print given range of lines using awk, perl, head, tail and python

print given range of lines - scripting

Print given range of lines using awk, perl, head, tail and python

Here is an example that, how we can print given range of lines using sed, awk, head, tail, perl and python methods.

In this article we are going to use rangoflines.txt file give a demonstration.

rangoflines.txt file contents

root@ankam-ubuntu:/home/aravi/scripts# cat rangoflines.txt
line 1
line 2
line 3
line 4
line 5
line 6
line 7
line 8
line 10
line 11
line 12
line 13
line 14
line 15
line 16
line 17
line 18
line 19
line 20
  1. Printing given range of lines using sed

Sed  is a stream editor.  A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline).

 $sed -n '5,9 p' rangoflines.txt

above is the sed command example to print the range from 5 to 10, out of 1-20 lines. -n option is to print ‘n’ number of lines.

in above example ‘p’ is used to Print the current pattern space.

exctract given range of lines sed

    $sed '5,9 !d' rangoflines.txt

Same as above we can also use ‘d’   Delete pattern space.  Start next cycle.

print given range of lines

2. Printing given range using awk ‘NR’ variable

Awk NR gives you the total number of records being processed or line number.

 $awk 'NR >= 5 && NR <= 9' rangoflines.txt

above example to use awk for printing the given range of lines, NR > greater then line number 5 and <= less then or equal to 10 line number we are printing using above example. We can also redirect to the any awk ‘NR >= 5 && NR <= 9’ rangoflines.txt > /tmp/files9

print given range of lines

3. Printing given range using head and tail commands

Using Linux/UNIX ‘head’ and ‘tail’ commands

$ head -9 rangoflines.txt | tail -5

We can also use below command to print the same

print given range of lines

$ head -9 rangoflines.txt | tail -$(((9-5)+1))

print given range of lines

as above example will also print the same from 5 to 10 lines out of 1-20 lines from rangoflines.txt file. head -9 will print 1-10 lines. tail -5 will print last 5 lines from it so the output is matching as per our requirements.

4. Printing given range of lines using vi editor

Open the rangoflines.txt file using vi editor

 $ vi rangoflines.txt

then enter into the EX mode by pressing Esc key then type the below line to copy the specified range lines to /tmp/file

:5,9 w! /tmp/file

i.e. Write lines between line number 5 and line number 9 of file ‘rangoflines.txt’ to file ‘/tmp/file’

5. Alternative using perl option to print given range of lines

$ perl -ne 'print if 5..9' rangoflines.txt

print given range of lines

as you can see above example perl command to using -ne option and print if condition to print the range of lines

6. Using python console also we can print the given range of lines

$ python
Python 2.5.2 (r252:60911, Jul 22 2009, 15:35:03)
[GCC 4.2.4 (Ubuntu 4.2.4-1ubuntu3)] on linux2

>>> fp = open("/tmp/file8","w")
>>> for i,line in enumerate(open("rangoflines.txt")):
...     if i >= 26 and i < 9 :
...             fp.write(line)

Please provide your valuable comments…

Thanks for your wonderful Support and Encouragement


Ankam Ravi Kumar

Working as Linux / Storage Administrator L3. Interested in sharing the knowledge.

2 Responses

  1. blank Preetham says:

    I would like to automate day to day work in Hitachi storage box . Please help me out doing it

Leave a Reply

Your email address will not be published. Required fields are marked *