On 5/13/08, Dana Merrick <dmerrick / ics.com> wrote: > Oh, sorry, I should have mentioned, I'm providing two regexes, and the > script is to replace a line that matches one regex with a line that matches > the other. > > I.E. > line one > line two > line three > > a=/one/ > b=/three/ > > And the script produces: > line three > line two > line one > > I intend on passing it robust enough regexes that they only match a single > line (i.e. not /line/). > > -Dana > > > Dana Merrick wrote: > > Hey guys, > > > > I'm trying to write a script to switch two lines in a file, and for some > reason am having difficulty grokking it. > > > > I have an ugly start, which I'll post if you want, but it's over thirty > lines long and still doesn't work. I'm sure there is a very short, very > simple, very Ruby solution to this, and I'm sure one of you has it. > > > > So I'm going to cheat and ask you guys. > > > > Any ideas? > > > > -Dana > > > > > > > > -- > Dana Merrick - System Administrator > Integrated Computer Solutions, Inc. > 54B Middlesex Tpke, Bedford, MA 01730 > 617.621.0060 x112 - http://www.ics.com > > > one somewhat inefficient possibility is: f= File.new(fname,File::RDWR) lines=f.readlines first = lines.index(lines.grep(/p1/)[0]) second = lines.index(lines.grep(/p2/)[0]) lines[first],lines[second]=lines[second],lines[first] f.rewind f.write lines f.close