# How to find at which line characters appear

**URL:** <https://www.ruby-forum.com/t/how-to-find-at-which-line-characters-appear/108579>\
**Category:** Ruby\
**Created:** [July 5, 2007, 12:02am UTC](https://www.ruby-forum.com/t/how-to-find-at-which-line-characters-appear/108579 "2007-07-05T00:02:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![desertfox](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8491ac/32.png) [@desertfox](https://www.ruby-forum.com/u/desertfox)\
**Post date:** [July 5, 2007, 12:02am UTC](https://www.ruby-forum.com/t/how-to-find-at-which-line-characters-appear/108579/1 "2007-07-05T00:02:14Z")

</div>

Hello,

I have a file that includes a “---------” string at a certain line  
number. How can I find out at which line number the string occurs?  
Would it be best to split the text file into an array with split("\n")  
and then use a for loop or is there some other hidden methods in ruby?

Basically I want to start reading a text file after the “---------” line  
that appears in it.

Can someone help please?

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![desertfox](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8491ac/32.png) [@desertfox](https://www.ruby-forum.com/u/desertfox)\
**Post date:** [July 5, 2007, 12:50am UTC](https://www.ruby-forum.com/t/how-to-find-at-which-line-characters-appear/108579/2 "2007-07-05T00:50:11Z")

</div>

f = File.open(‘filename’)  
until f.gets.chomp == “---------”; end  
#continue processing here

That should do the trick. The next line returned by f.gets will be the  
first line after the “--------”. If you want the actual line number,  
f.lineno should give it to you.

-Brett

---

<div class="post-metadata">

**Author:** ![desertfox](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8491ac/32.png) [@desertfox](https://www.ruby-forum.com/u/desertfox)\
**Post date:** [July 5, 2007, 1:08am UTC](https://www.ruby-forum.com/t/how-to-find-at-which-line-characters-appear/108579/3 "2007-07-05T01:08:34Z")

</div>

Al Cholic wrote:

> Hello,
> 
> I have a file that includes a “---------” string at a certain line  
> number. How can I find out at which line number the string occurs?  
> Would it be best to split the text file into an array with split("\n")  
> and then use a for loop or is there some other hidden methods in ruby?
> 
> Basically I want to start reading a text file after the “---------” line  
> that appears in it.
> 
> Can someone help please?
> 
> Thanks in advance.

2 methods come to my mind to do that:

1. as you said, split and find what index in the array contains the line
2. get the offset (String#index) of the first “---------”, count the  
"\n"s in the substring 0,offset.  
I’d assume that the second is faster.

Regards  
Stefan

---

<div class="post-metadata">

**Author:** ![desertfox](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8491ac/32.png) [@desertfox](https://www.ruby-forum.com/u/desertfox)\
**Post date:** [July 5, 2007, 1:13am UTC](https://www.ruby-forum.com/t/how-to-find-at-which-line-characters-appear/108579/4 "2007-07-05T01:13:32Z")

</div>

Whoops, I should have read this paragraph too:

> > Basically I want to start reading a text file after the “---------” line  
> > that appears in it.

Then of course I’d do it just as Brett already said: read until you get  
there not processing the data, then continue to read, this time  
processing the data.

Regards  
Stefan

---

<div class="post-metadata">

**Author:** ![desertfox](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8491ac/32.png) [@desertfox](https://www.ruby-forum.com/u/desertfox)\
**Post date:** [July 5, 2007, 5:10am UTC](https://www.ruby-forum.com/t/how-to-find-at-which-line-characters-appear/108579/5 "2007-07-05T05:10:12Z")

</div>

Stefan R. wrote:

> Whoops, I should have read this paragraph too:
> 
> > > Basically I want to start reading a text file after the “---------” line  
> > > that appears in it.
> 
> Then of course I’d do it just as Brett already said: read until you get  
> there not processing the data, then continue to read, this time  
> processing the data.
> 
> Regards  
> Stefan

Thanks for guys’ advice. I also came up with another way to get the  
text.

def find\_component\_list(text)  
/\_\_\_+/.match(text).post\_match.strip.split("\n")  
end

Im using a regular expression.

How good is this in terms of efficiency compared to the other methods?
