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

Douglas McIlroy douglas.mcilroy at dartmouth.edu
Fri Jul 16 22:09:59 AEST 2021


>> -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.

Doug


More information about the TUHS mailing list