[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Can you get the 1st occurrence of a value using grep?

Mmcolli00 Mom

5/14/2009 8:30:00 PM

Can you get a 1st occurence of a sting inside a huge text file?

For instance, I know that there will be 1 or more occurrences of a the
string 'fail' in a text file and I only want to get the first line
containing the 'fail' instead of getting every line with a fail. My
program reads over 100 files that may contain one or more 'fail' values.

Thanks
MC

Dir["//MYFile/*.txt"].each do |textfile|
getGrepOffense = textfile.grep(/fail/)
strOffense = getGrepOffense.join
puts strOffense
end
--
Posted via http://www.ruby-....

3 Answers

pat eyler

5/14/2009 8:44:00 PM

0

lines.detect {|l| l =3D~ /fail/}


On Thu, May 14, 2009 at 2:30 PM, Mmcolli00 Mom <mmc_collins@yahoo.com> wrot=
e:
> Can you get a 1st occurence of a sting inside a huge text file?
>
> For instance, I =A0know that there will be 1 or more occurrences of a the
> string 'fail' in a text file and I only want to get the first line
> containing the 'fail' instead of getting every line with a fail. My
> program reads over 100 files that may contain one or more 'fail' values.
>
> Thanks
> MC
>
> Dir["//MYFile/*.txt"].each do |textfile|
> =A0 =A0getGrepOffense =3D textfile.grep(/fail/)
> =A0 =A0strOffense =3D getGrepOffense.join
> =A0 =A0puts strOffense
> end
> --
> Posted via http://www.ruby-....
>
>



--=20
thanks,
-pate
-------------------------
Don't judge those who choose to sin differently than you do

http://on-ruby.bl...
http://eldersjournal.bl...

Mmcolli00 Mom

5/14/2009 9:21:00 PM

0

Cool thanks! -MC
--
Posted via http://www.ruby-....

Joel VanderWerf

5/14/2009 9:30:00 PM

0

Mmcolli00 Mom wrote:
> Can you get a 1st occurence of a sting inside a huge text file?
>
> For instance, I know that there will be 1 or more occurrences of a the
> string 'fail' in a text file and I only want to get the first line
> containing the 'fail' instead of getting every line with a fail. My
> program reads over 100 files that may contain one or more 'fail' values.
>
> Thanks
> MC
>
> Dir["//MYFile/*.txt"].each do |textfile|
> getGrepOffense = textfile.grep(/fail/)
> strOffense = getGrepOffense.join
> puts strOffense
> end

[~/tmp] cat grepfail.rb
DATA.grep /fail/ do |line|
p line
break
end

__END__
ok 1
ok 2
fail 1
ok 3
fail 2
fail 3
[~/tmp] ruby grepfail.rb
"fail 1\n"

This should work with an open File in place of DATA.

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407