[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Can one use line addresses...to select a portion of file

Gregory Seidman

12/3/2006 6:50:00 PM

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


4 Answers

Jacob, Raymond A Jr

12/3/2006 7:33:00 PM

0

To: Gregory Seidman ] and David Vallner
Thank you,
Raymond
-----Original Message-----
From: Gregory Seidman [mailto:gsslist+ruby@anthropohedron.net]
Sent: Sunday, December 03, 2006 13:50
To: ruby-talk ML
Subject: Re: Can one use line addresses...to select a portion of file

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


Devin Mullins

12/4/2006 3:36:00 AM

0

Gregory Seidman 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

David Vallner

12/5/2006 10:10:00 PM

0

Devin Mullins wrote:
> Gregory Seidman 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
>

So -this- is what that does?

Does this do that?

Does anyone know what that notation does?

David Vallner
Incoherent

Ara.T.Howard

12/5/2006 10:21:00 PM

0