I would like to select a portion of a file to process like I did with
sed.
i.e. a file called Menu with the following contents:
McDonalds
…
…
…(stuff deleted)
Happy_Meal_
With sed I would do something like
sed -n “/McDonalds/,/_Happy_Meal/p” Menu
I can not find anything comparable in Ruby. I may be using the wrong
words when I searched google for ruby line address.
Any help will be appreciated.
Thank you,
Raymond
Jacob, Raymond A Jr wrote:
Happy_Meal_
Raymond
For a trivial approach, and the most terse one:
lines = file.to_a
lines_to_process = lines[(lines.index(“McDonalds\n”)) …
lines.index(“Happy_Meal\n”))]
Requires loading the whole file into memory, can only, and several
passes through it, so the performance is probably ugly, can only handle
lines, and can’t cope with regular expressions (which is why you need
the \n in the delimiters). But probably Good Enough for some
applications.
A more involved solution would probably be one yielding lines between
lines that match the delimiters to a block or returning an array of them
if no block was given. The most flexible one would be “faking” an IO
object that would start at the starting pattern and then look for the
ending one as it goes, but unless you need the data without line breaks
and without the memory overhead of keeping them in memory, probably too
much bother.
David V.
On Mon, Dec 04, 2006 at 03:19:25AM +0900, Jacob, Raymond A Jr wrote:
} I would like to select a portion of a file to process like I did with
} sed.
}
} i.e. a file called Menu with the following contents:
} McDonalds
}
} …
} …
} …(stuff deleted)
}
} Happy_Meal
}
} With sed I would do something like
}
} sed -n “/McDonalds/,/Happy_Meal/p” Menu
}
} I can not find anything comparable in Ruby. I may be using the wrong
} words when I searched google for ruby line address.
} Any help will be appreciated.
Well, sometimes sed (or awk, which does the same thing even more simply:
awk ‘/McDonalds/,/Happy_Meal/’) is the right tool. If you are
selected this subset of lines for further processing in ruby, however
try
this:
inrange = false
lines = open(file).read.split("\n").select { |line|
inrange &&= not /Happy_Meal/ === line
inrange ||= /McDonalds/ === line
}
} Thank you,
} Raymond
–Greg
Gregory S. wrote:
inrange = false
lines = open(file).read.split("\n").select { |line|
inrange &&= not /Happy_Meal/ === line
inrange ||= /McDonalds/ === line
}
There is a lesser-known (and usually frowned upon?) feature in Ruby that
treats Range notation differently in a “boolean context.”
IO.foreach(file) do |line| # use this rather that loading the whole file
if line =~ /McDonalds/ … line =~ /Happy_Meal/
do_stuff_with line
end
end
Devin
Devin M. wrote:
if line =~ /McDonalds/ … line =~ /Happy_Meal/
do_stuff_with line
end
end
So -this- is what that does?
Does this do that?
Does anyone know what that notation does?
David V.
Incoherent
To: Gregory S. ] and David V.
Thank you,
Raymond
On Wed, 6 Dec 2006, David V. wrote:
So -this- is what that does?
Does this do that?
yes.
Does anyone know what that notation does?
it’s a boolean toggle.
well doccumented in pickaxe and every perl book out there - if not
somewhat
esoteric.
cheers.
-a