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
methyl
February 20, 2012, 1:50pm
3
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... ... last 1/2 hour i was trying to achieve this by tail ..only ..
--Shirish
methyl
February 20, 2012, 10:18pm
6
@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.
alister
February 20, 2012, 10:43pm
7
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:
Tue Apr 14 17:34:54 PDT 1998 Ken Pizzini <ken@gnu.org>
* execute.c \(execute_program, process_files, count_branches,
shrink_program\): Added a first attempt at program optimization.
We now can quit early if we are running with the "-n"
and all of the commands are known to be valid only for
lines less than the current line. Thus the "sed" in
"foo | sed -n 1,2p" will print read three lines, printint
the first two, and then quit, regardless of how much longer
"foo" might run or output. This optimization does not buy
much in most cases \(it sometimes even costs a little\),
but when it does help it can help big. The code is
all conditionally compiled based on the
EXPERIMENTAL\_DASH\_N_OPTIMIZATION symbol being \#defined,
so it can be easily omitted if it causes problems.
... snip ....
Sat May 23 16:07:09 HST 1998 Ken Pizzini <ken@gnu.org>
* sed/execute.c: \#undef'd EXPERIMENTAL\_DASH\_N_OPTIMIZATION
because its code is buggy.
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
methyl
February 21, 2012, 9:44am
9
@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