From: "Bermejo, Rodrigo" Date: 2003-12-16T07:24:34+09:00 Subject: Re: Extracting multiple lines from a file stuff=%r|^BEGIN_FOO$(.+)^END_FOO$|m file=File.new("file","r").read file =~ stuff p clean_stuff = $1 Ron Coutts wrote: > > >>-----Original Message----- >>From: Berger, Daniel [mailto:djberge@qwest.com] >>Sent: December 15, 2003 2:53 PM >>To: ruby-talk ML >>Subject: Re: Extracting multiple lines from a file >> >> >> >> >>>-----Original Message----- >>>From: Ron Coutts [mailto:rcoutts@envistatech.com] >>>Sent: Monday, December 15, 2003 2:14 PM >>>To: ruby-talk ML >>>Subject: Extracting multiple lines from a file >>> >>> >>>This should be an easy question for Ruby-heads but I can't >>>seem to easily find the answer. I need to extract multiple >>>lines from a file where the lines are delimited by some other >>>text. For example, with file contents as shown below I'd >>>like to extract the content between the BEGIN_FOO and END_FOO >>>lines. Can anyone tell me how to do that? >>> >>>BEGIN_FOO >>>Some foo stuff here. >>>More foo stuff. >>>END_FOO >>> >>>I've done this before in Perl (can't remember the syntax) so >>>I'm thinking it should be possible in Ruby. >>> >>>Ron >>> >>> >>See the second argument to IO#readlines. Or, you can set $/ >>manually - >>same as Perl. :) >> >>Regards, >> >>Dan >> >> > >Thanks for the quick reply Dan, but I may not have been clear as to what >I'm trying to do. IO#readlines allows one to read lines and specify the >line delimiter (which is usually '\n'). I'm trying to extract the >content between the lines BEGIN_FOO and END_FOO within a file where the >lines are delimited with '\n'. > >As I look at the problem further, it seems like Ranges may be part of >the solution. In the Pickaxe book by Dave Thomas and Andrew Hunt they >give this example where a file (named "ordinal") containing the words >'one' through 'ten', one word per line, is parsed (page 84): > >file = File.open("ordinal") >while file.gets > print if /third/ .. /fifth/ >End > >Unfortunately this code snippet is buggy as it prints out all ten lines >of the file, not just lines three through five as mentioned in the book. >So I'm still stuck on this problem. > >I've seen Dave Thomas post on the list. If you read this Dave, do you >have any pointers for me or the fix for the code snippet above? Any >help would be appreciated. > >Thanks, >Ron > > > > > -- General Electric - CIAT Advanced Engineering Center ________________________________________ Rodrigo Bermejo Information Technologies. Special Applications Dial-comm : *879-0644 Phone :(+52) 442-196-0644