[Nottingham] simple question?

Martin martin at ml1.co.uk
Wed Oct 25 11:08:12 BST 2006


Martin Garton wrote:
> Hi,
> 
> I'm sure this should be trivially simple and I should already know the
> answer, but what's an easy way to reverse the order of the lines in a
> text file with a filter command?  (preferably without involving perl)

You could try this:

#!/usr/bin/sed -nf


      # reverse all lines of input, i.e. first line became last, ...


      # from the second line, the buffer (which contains all previous lines)
      # is *appended* to current line, so, the order will be reversed
      1! G


      # on the last line we're done -- print everything
      $ p


      # store everything on the buffer again
      h


And then there is also "tac" as opposed to "cat"...


Have fun,
Martin


-- 
----------------
Martin Lomas
martin at ml1.co.uk
----------------



More information about the Nottingham mailing list