# How to find second match in a file

**URL:** <https://www.ruby-forum.com/t/how-to-find-second-match-in-a-file/141452>\
**Category:** Ruby\
**Created:** [June 25, 2008, 11:55pm UTC](https://www.ruby-forum.com/t/how-to-find-second-match-in-a-file/141452 "2008-06-25T23:55:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cheyne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b9bd4f/32.png) [@cheyne](https://www.ruby-forum.com/u/cheyne)\
**Post date:** [June 25, 2008, 11:55pm UTC](https://www.ruby-forum.com/t/how-to-find-second-match-in-a-file/141452/1 "2008-06-25T23:55:37Z")

</div>

Hi, everyone

My code opens a file and read line by line to find a given string as  
first match. After I found one, how can i find the second match, a  
different string, which is supposed to appear several lines after the  
line of first match in a fast way?

for example, the following is the contents of a file

ka;sdfjaskf;asdfjk  
skdfkfkfkfkfkfkfkfkfk  
aks;flasjklfjkassjfksa  
aaaaaaaaaaaaaaaaaaaaaaaa  
bbbbbasdfdask;fjsdkalf  
_ **hello world** _\*\*ka;sdjkf;sjadkfj  
asdfkasjdkfldjaskl;jfklas;  
jaskdl;fjkasdjkfljdskla;jdfs  
jkasl;jfkasjdklfjsal;df  
asjkdfjkasd;fd;asdfj  
\*\*_ **Ruby** _a;sjdfklsdjal;fjkda;  
asdfjjasklfjkdasjkfjaskl;  
fjdka;dfdjasjfsd;a

So, after I found the match “hello world” by using a while loop, how can  
i find the “Ruby” in the same while loop?

Looking forward to your reply. Thanks in advance

---

<div class="post-metadata">

**Author:** ![cheyne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b9bd4f/32.png) [@cheyne](https://www.ruby-forum.com/u/cheyne)\
**Post date:** [June 26, 2008, 2:44am UTC](https://www.ruby-forum.com/t/how-to-find-second-match-in-a-file/141452/2 "2008-06-26T02:44:46Z")

</div>

Cheyne Li wrote:

> Hi, everyone
> 
> My code opens a file and read line by line to find a given string as  
> first match. After I found one, how can i find the second match, a  
> different string, which is supposed to appear several lines after the  
> line of first match in a fast way?
> 
> for example, the following is the contents of a file
> 
> ka;sdfjaskf;asdfjk  
> skdfkfkfkfkfkfkfkfkfk  
> aks;flasjklfjkassjfksa  
> aaaaaaaaaaaaaaaaaaaaaaaa  
> bbbbbasdfdask;fjsdkalf  
> _ **hello world** _\*\*ka;sdjkf;sjadkfj  
> asdfkasjdkfldjaskl;jfklas;  
> jaskdl;fjkasdjkfljdskla;jdfs  
> jkasl;jfkasjdklfjsal;df  
> asjkdfjkasd;fd;asdfj  
> \*\*_ **Ruby** _a;sjdfklsdjal;fjkda;  
> asdfjjasklfjkdasjkfjaskl;  
> fjdka;dfdjasjfsd;a
> 
> So, after I found the match “hello world” by using a while loop, how can  
> i find the “Ruby” in the same while loop?
> 
> Looking forward to your reply. Thanks in advance  
> Not sure why you need it …but seems you can do what you need by simple  
> doing this:  
> File.readlines(“1.txt”).each do |line|  
> if line =~ /hello world/  
> #do something  
> p line  
> end  
> if line =~ /Ruby/  
> #do something else  
> p line  
> end  
> end

---

<div class="post-metadata">

**Author:** ![cheyne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b9bd4f/32.png) [@cheyne](https://www.ruby-forum.com/u/cheyne)\
**Post date:** [June 26, 2008, 3:33pm UTC](https://www.ruby-forum.com/t/how-to-find-second-match-in-a-file/141452/3 "2008-06-26T15:33:02Z")

</div>

2008/6/25 Cheyne Li [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> skdfkfkfkfkfkfkfkfkfk  
> fjdka;dfdjasjfsd;a
> 
> So, after I found the match “hello world” by using a while loop, how can  
> i find the “Ruby” in the same while loop?

It’s probably easiest if you store the state of your search somewhere.

state = :initial

File.foreach “foo” do |line|  
case state  
when :initial  
if /first/ =~ line  
puts line  
state = :first\_match  
end  
when :first\_match  
if /second/ =~ line  
puts line  
state = :second\_match  
end  
else  
# ignore  
end  
end

Cheers

robert
