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

**URL:** <https://www.ruby-forum.com/t/can-you-get-the-1st-occurrence-of-a-value-using-grep/168008>\
**Category:** Ruby\
**Created:** [May 14, 2009, 10:30pm UTC](https://www.ruby-forum.com/t/can-you-get-the-1st-occurrence-of-a-value-using-grep/168008 "2009-05-14T22:30:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mmcolli00](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@mmcolli00](https://www.ruby-forum.com/u/mmcolli00)\
**Post date:** [May 14, 2009, 10:30pm UTC](https://www.ruby-forum.com/t/can-you-get-the-1st-occurrence-of-a-value-using-grep/168008/1 "2009-05-14T22:30:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mmcolli00](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@mmcolli00](https://www.ruby-forum.com/u/mmcolli00)\
**Post date:** [May 14, 2009, 10:45pm UTC](https://www.ruby-forum.com/t/can-you-get-the-1st-occurrence-of-a-value-using-grep/168008/2 "2009-05-14T22:45:10Z")

</div>

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

On Thu, May 14, 2009 at 2:30 PM, Mmcolli00 Mom [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ## Dir[“//MYFile/\*.txt”].each do |textfile| getGrepOffense = textfile.grep(/fail/) strOffense = getGrepOffense.join puts strOffense end
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

## – thanks, -pate

Don’t judge those who choose to sin differently than you do

> **[On Ruby](http://on-ruby.blogspot.com)**
>
> The (mostly) tech related musings of Pat Eyler. Ruby, Erlang, Haskell, Scala, Ocaml, Publishing, and more ...

> **[Elders Journal](http://eldersjournal.blogspot.com)**
>
> Random musings from a faithful Latter-day Saint and his family

---

<div class="post-metadata">

**Author:** ![mmcolli00](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@mmcolli00](https://www.ruby-forum.com/u/mmcolli00)\
**Post date:** [May 14, 2009, 11:20pm UTC](https://www.ruby-forum.com/t/can-you-get-the-1st-occurrence-of-a-value-using-grep/168008/3 "2009-05-14T23:20:41Z")

</div>

Cool thanks! -MC

---

<div class="post-metadata">

**Author:** ![mmcolli00](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@mmcolli00](https://www.ruby-forum.com/u/mmcolli00)\
**Post date:** [May 14, 2009, 11:30pm UTC](https://www.ruby-forum.com/t/can-you-get-the-1st-occurrence-of-a-value-using-grep/168008/4 "2009-05-14T23:30:56Z")

</div>

Mmcolli00 Mom wrote:

> 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.
