On Tuesday 04 September 2007 14:31, Rich Smrcina wrote:
>He wants to discard the first and last 10,000 lines.  head and tail
>write them to stdout.

Doh!  I misread it.  Sorry about that.  I'm usually trying to preserve the
last N lines of my logs, so I wrote that reflexively.

Mark's method using sed is the best approach, though I'd probably calculate
the starting line number using awk:

start=$(awk 'END {s=NR-10000; if (s < 1) s=1; print s}' /var/log/toolarge)
sed -i -e "$start",'$ d' /var/log/toolarge

You could actually do the whole thing in awk using a circular buffer of 10000
lines, and that might be more efficient because it makes only one pass
through the input file:

awk 'BEGIN {N=10000} \
        {if (p) print Lines[i]; Lines[i++] = $0; if (i == N) {i=0; p=1}}' \
                /var/log/toolarge

That's a bit cryptic, but it is just printing the 10000th line before the one
it is reading.  It works by buffering up 10000 lines and turning on printing
when the buffer circles around to overwrite the first line.  Awk Rules!

Oh well.  Even if I can't read the question right, I can still contribute
something. :-)
        - MacK.
-----
Edmund R. MacKenty
Software Architect
Rocket Software, Inc.
Newton, MA USA

----------------------------------------------------------------------
For LINUX-390 subscribe / signoff / archive access instructions,
send email to [EMAIL PROTECTED] with the message: INFO LINUX-390 or visit
http://www.marist.edu/htbin/wlvindex?LINUX-390

Reply via email to