From: Vandana on
Hello All,

I would like to read a file in ruby. It is a 2G file, but
contain useless data in the beginning portion of the file.

There is a particular pattern towards the middle of the file after
which useful data begins. Is there a way to grep for this pattern and
then read every line henceforth, but ignore all lines previous to line
on which pattern found?

Thanks,
Vandana
From: Thomas Volkmar Worm on
File.open("myfile", "r") do |f|

# Skip the garbage before pattern:
while f.gets !~ /pattern/ do; end

# Read your data:
while l = f.gets
puts l
end

end
From: Vandana on
Thank you very much.


On May 12, 4:18 pm, Thomas Volkmar Worm <t...(a)s4r.de> wrote:
> File.open("myfile", "r") do |f|
>
>   # Skip the garbage before pattern:
>   while f.gets !~ /pattern/ do; end
>
>   # Read your data:
>   while l = f.gets
>     puts l
>   end
>
> end

From: Robert Klemme on
On 05/13/2010 02:40 AM, Vandana wrote:

> On May 12, 4:18 pm, Thomas Volkmar Worm <t...(a)s4r.de> wrote:
>> File.open("myfile", "r") do |f|
>>
>> # Skip the garbage before pattern:
>> while f.gets !~ /pattern/ do; end
>>
>> # Read your data:
>> while l = f.gets
>> puts l
>> end
>>
>> end

There's also the flip flop operator:

File.foreach "myfile" do |line|
if /pattern/ =~ line .. false
puts line
end
end

The trick I am using is that the FF operator starts to return true if
the first expression returns true and stays true until the last
expression returns true - in this case never since you want to read
until the end of the file.

Kind regards

robert

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
From: =?ISO-8859-1?Q?Une_B=E9vue?= on
Robert Klemme <shortcutter(a)googlemail.com> wrote:

> There's also the flip flop operator:
>
> File.foreach "myfile" do |line|
> if /pattern/ =~ line .. false
> puts line
> end
> end
>
> The trick I am using is that the FF operator starts to return true if
> the first expression returns true and stays true until the last
> expression returns true - in this case never since you want to read
> until the end of the file.

coud that trick be used for start and stop tags ? like :

File.foreach "myfile" do |line|
if /<body/ =~ line .. /<\/body/ =~ line
puts line
end
end

if true, that's clever !
--
� La vie ne se comprend que par un retour en arri�re,
mais on ne la vit qu'en avant. �
(S�ren Kierkegaard)