[TUHS] head/sed/tail (was The Unix shell: a 50-year view)

Bakul Shah bakul at iitbombay.org
Sat Jul 17 00:32:05 AEST 2021


On Jul 16, 2021, at 5:09 AM, Douglas McIlroy <douglas.mcilroy at dartmouth.edu> wrote:
> 
>>> -r is weird because it enables backwards reading, but only as
>>> limited by count. Better would be a program, say revfile, that simply
>>> reads backwards by lines. Then tail p  has an elegant implementation:
>>>     revfile p | head | revfile
> 
>> tail -n can be smarter in that it can simply read the last K bytes
>> and see if there are n lines. If not, it can read back further.
>> revfile would have to read the whole file, which could be a lot
>> more than n lines! tail -n < /dev/tty may never terminate but it
>> will use a small finite amount of memory.
> 
> Revfile would work the same way. When head has seen enough
> and terminates, revfile will get SIGPIPE and stop. I agree that,
> depending on scheduling and buffer management, revfile might
> read more than tail -n,  but  it wouldn't read the whole of a
> humongous file.

Good point! But when the input is from a device it would have to
buffer up everything since it doesn't know how much head would
want. No big deal of course but I was just pointing out that tail
can "behave better" in all cases!

-- Bakul



More information about the TUHS mailing list