Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!rutgers!njin!princeton!njsmu!mccc!jonlab!jon From: jon@jonlab.UUCP (Jon H. LaBadie) Newsgroups: comp.unix.questions Subject: Re: sorting and reversing lines of a file Summary: use a simple editor script Keywords: sort reverse Message-ID: <625@jonlab.UUCP> Date: 30 Jan 89 06:03:28 GMT References: <9056@burdvax.PRC.Unisys.COM> Organization: 4455 Province Line Rd., Princeton, NJ 08540 Lines: 36 In article <9056@burdvax.PRC.Unisys.COM>, lang@pearl.PRC.Unisys.COM (Francois-Michel Lang) writes: > > I need utilities to do two things: > (1) reverse the order of lines in a file > but leave the lines themselves intact. > E.g., if the file "f" contains > line1 > line2 > line3 > I want to produce > line 3 > line 2 > line 1 > Try the global command of ed(1), or ex/vi(1). If vi is your interactive editor, just type (from anywhere in the file): :g/^/m0 This globally finds every line with a beginning, and one at a time, moves them to the beginning of the file. Because of the speed, I'm sure the data is not reorganized until it is written back to the file. If you need to do this non-interactively, try a shell script invoking ed with a "here document". Something like this should work. ed - ${1:?"Need a file name"} <<-! g/^/m0 w q ! Put it in a file called flip, and flip those lines. -- Jon LaBadie {att, ulysses, princeton, bcr}!jonlab!jon