The pithy ruminations from Edward Ned Harvey <lop...@nedharvey.com>
with the subject "Re: [lopsa-discuss] Stupid sed question, or, Is sed
even the smartest way to do this?" were:

=> > From: discuss-boun...@lopsa.org [mailto:discuss-boun...@lopsa.org]
On => > Behalf Of Trey Darley

Let's => > say it's a logfile. Suppose that you wanted to access an
arbitrary => > range
=> > of lines, say, lines 10,000 - 13,000. One way of doing this is:
=> > 
=> > <snip>
=> > sed -n 10000,13000p foobar.txt
=> > </snip>

=> 
=> Inside a process, the process can seek() to a specific byte number
in a => file, but if you're looking for a specific range of line
numbers, and the => line length is variable, then there's no way to
jump directly to a specific => line number.  The only option is to do
as you're doing - read from the => beginning of the file, and scan for
\n characters and count line numbers. => 
=> It's possible, maybe, that sed has an inefficient algorithm to do
this.  But => I doubt it.
=> 

I'd also guess that sed is going to be the best balance of program
efficiency and transparency (for the admin, for troubleshooting, for
future maintenance).

If the performance is still unacceptable, then I'd consider writing a
program that does something like this horrible mish-mash of pseudo code:

=================================================

LINESZ=80       # emperically determined average line
                # length of logfile
 
start=argv[0]
end=argv[1]

start=start-5   # provide some overlap, adjust as needed
end=end+5

# turn user-supplied start & end line numbers into byte
# range
start=start*LINESZ
end=end*LINESZ

logfile=open(LOGFILE)
seek(start) # seek() to the approx line

# skip to the next new line
while(currentchar != '\n')
{
        currentchar=read(start)
        start++
}
start++

# print data in the requested range
while(start <= end)
{
        printf('%s',read(start))
        start++
}

# print to the end of the current line
while(currentchar != '\n')
{
        currentchar=read(start)
        printf('%s',currentchar)
        start++
}
printf('\n')
====================================================


Having typed that horrible example, I'd still use sed.

Mark


-----
Mark Bergman    Biker, Rock Climber, Unix mechanic, IATSE #1 Stagehand
'94 Yamaha GTS1000A^2
berg...@panix.com

http://wwwkeys.pgp.net:11371/pks/lookup?op=get&search=bergman%40panix.com

I want a newsgroup with a infinite S/N ratio! Now taking CFV on:
rec.motorcycles.stagehands.pet-bird-owners.pinballers.unix-supporters
15+ So Far--Want to join? Check out: http://www.panix.com/~bergman 
_______________________________________________
Discuss mailing list
Discuss@lopsa.org
http://lopsa.org/cgi-bin/mailman/listinfo/discuss
This list provided by the League of Professional System Administrators
 http://lopsa.org/

Reply via email to