print from - to line of a file

Need to print file contents from line number A to line-number B

eg:

 
# cat -n a
     1  aaaaaaaaaaaaa
     2  bbbbbbbbbbbbb
     3  ccccccccccccc
     4  ddddddddddddd
     5  eeeeeeeeeeeee
     6  ffffffffffffff
     7  gggggggggggg

## Need to print contents from linenum 3-6 only it may be big file of 20MB 
 
## I know to tail from line number 
# tail -n+3  a   --> But ther's no option for to line number ..

--Shirish

awk '(NR>=3 && NR<=6){print NR "  " $0}' f1

---------- Post updated at 01:23 PM ---------- Previous update was at 01:22 PM ----------

here f1 is the name of file

The awk in post #2 will read the whole file.

This combination of "head" and "tail" would be much quicker on a large file.

cat -n a | head -6 | tail -4

Thanks Methyl !!

That's the nice tips... :slight_smile: ... last 1/2 hour i was trying to achieve this by tail ..only ..

--Shirish

sed -n 3,6p infile

Large files:

sed -n '3,6p;6q' infile

@Scrutinizer that is an excellent post.

I knew about:

But it goes on to read the whole file.

But this piece of brilliant code does not feature in any of the many "sed one-liners" lists which I have read and it does not read the whole file. Therefore it is quick.

GNU attempted to automagically optimize this very case, but apparently never managed to shake out all the bugs.

From http://git.savannah.gnu.org/cgit/sed.git/tree/ChangeLog:

Many years later, in http://git.savannah.gnu.org/cgit/sed.git/tree/sed/execute.c\#n19

#undef EXPERIMENTAL_DASH_N_OPTIMIZATION	/*don't use -- is very buggy*/

Regards,
Alister

Thanks Methyl & Scrutinizer!!

Big time diff have found with both the cmd's ..

[root@shirish-nagios ~]# du -sh msg
1.9G    msg
[root@shirish-nagios ~]#
[root@shirish-nagios ~]# time sed -n '777751,777753p;777753q' msg
Feb 21 19:53:37 shirish-nagios nagios: HOST NOTIFICATION: nagiosadmin;SEDGE2;DOWN;notify-host-by-email;CRITICAL - Network Unreachable 
Feb 21 19:53:37 shirish-nagios nagios: HOST ALERT: VRB3N;DOWN;HARD;10;CRITICAL - Network Unreachable (VRB3N)
Feb 21 19:53:37 shirish-nagios nagios: HOST NOTIFICATION: linux-admin;VRB3N;DOWN;notify-host-by-email;CRITICAL - Network Unreachable 

real    0m0.290s
user    0m0.075s
sys     0m0.087s
[root@shirish-nagios ~]# time sed -n 777751,777753p msg
Feb 21 19:53:37 shirish-nagios nagios: HOST NOTIFICATION: nagiosadmin;SEDGE2;DOWN;notify-host-by-email;CRITICAL - Network Unreachable
Feb 21 19:53:37 shirish-nagios nagios: HOST ALERT: VRB3N;DOWN;HARD;10;CRITICAL - Network Unreachable (VRB3N)
Feb 21 19:53:37 shirish-nagios nagios: HOST NOTIFICATION: linux-admin;VRB3N;DOWN;notify-host-by-email;CRITICAL - Network Unreachable 

real    0m19.106s
user    0m0.986s
sys     0m5.781s

--Shirish Shukla

@Shirishlnx
For the future please post realistic example making the scale clear.

I would certainly not use the "head and tail" approach on a large log file.

Sure .. !! That's I mentioned in my first thread that file size may be big as 20 MB ...

Here to test the time behavior with above sed codes ... have created the file of 1.9GB ..

Thanks Methyl .. Definitely will take care this !!!

--Shirish Shukla