Switch Two Lines in a File

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

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

On 5/13/08, Dana M. [email protected] wrote:

b=/three/

Dana M. - System A.
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

Dana M. wrote:

I’m providing two regexes, and the
script is to replace a line that matches one regex with a line that
matches the other.

Here’s my solution; thanks for the fun exercise.

$ cat foo
line three
line two
line one
another one
another three

$ ruby -v switch.rb foo
ruby 1.8.6 (2007-09-24 patchlevel 111) [i686-linux]
line one
line two
line three
another three
another one

$ cat switch.rb
firstQuery = /one/
secondQuery = /three/

determine line numbers of matching lines

firstIndices = []
secondIndices = []

lines = ARGF.readlines
lines.each_with_index do |line, i|
case line
when firstQuery
firstIndices << i
when secondQuery
secondIndices << i
end
end

swap all matching lines

firstIndices.zip(secondIndices).each do |(a, b)|
lines[a], lines[b] = lines[b], lines[a]
end

puts lines

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs