Reversing a file?

Richard O'Keefe ok at cs.mu.oz.au
Sun Oct 8 21:31:27 AEST 1989


In article <810 at jonlab.UUCP>, jon at jonlab.UUCP (Jon LaBadie) writes:
> However, in this case, no one seems to have posted the (IMHO) elegant
> solution I would propose.  Thus, my suggestion for:
> 	HOW DO YOU REVERSE THE LINES OF A FILE?
> ed - ${1} <<!
>     g/^/m0
>     w
>     q
> !
> Any questions?

No questions, but several comments.
(a) This method, like most others that have been posted, assumes that
    your virtual memory is at least as big as your file.  That's not
    really an elegant assumption.  (I occasionally handled 1/2M files
    on a PDP-11 with a massive 64k of virtual memory -- no separate I/D.)
    It's a particularly bad assumption when you bring ed(1) into it,
    because ed tends to live in the past (be compiled with limits
    appropriate to a PDP-11 rather than an 80386).  For example:
	ed may strip off the 8th bit of characters
	ed may truncate lines to 512 characters
	ed may limit its "work file" to 64k or 128k characters
	ed may not handle long file names (64 character limit sometimes)
	ed may just plain not work
    All of these have hit me in real UNIX releases as provided by vendors.
    (Not all at the same time.)

(b) This method requires the text to already be in a file; it can't be
    used with a pipe.  (A temporary file can be used, but do remember to
    try $TMPDIR rather than assuming /tmp and do remember to rm it.)

(c) The reversed lines are written back onto the original file.  That's
    not necessarily a good idea.  The file might be write protected.
    The intended user might not want that.

[d: strange things will happen if any input lines contain NULs, but that
    also applies to my solution using sort(1), and to any UNIX utility
    that reads its input with fgets().]

In general, The UNIX Way of doing something like this is to make it look
as much like a filter as possible.



More information about the Comp.unix.questions mailing list