Mimick tac with python.

Christian Gollwitzer auriocus at gmx.de
Sat Jan 30 04:23:34 EST 2016


Am 30.01.16 um 08:56 schrieb Jussi Piitulainen:
> Christian Gollwitzer writes:
>
>> Am 30.01.16 um 05:58 schrieb Random832:
>>> On Fri, Jan 29, 2016, at 23:46, Hongyi Zhao wrote:
>>>> awk '{a[NR]=$0} END {while (NR) print a[NR--]}' input_file
>>>> perl -e 'print reverse<>' input_file
>>>
>>> Well, both of those read the whole file into memory - tac is sometimes
>>> smarter than that, but that makes for a more complex program.
>>
>> Now I'm curious. How is it possible to output the first line as last
>> again if not by remembering it from the every beginning? How could tac
>> be implemented other than sucking up everything into memory?
>
> It may be possible to map the data into virtual memory so that the
> program sees it as an array of bytes. The data is paged in when
> accessed. The program just scans the array backwards, looking for
> end-of-line characters. I believe they can be identified reliably, as
> bytes, even in a backward scan of UTF-8-encoded data.
>
> The data needs to be in a file.

If it's in a file, then I agree. I was thinking about the case where tac 
is used in a pipe - obviously here you can't reverse the file in 
constant memory.

	Christian



More information about the Python-list mailing list