# Finding a range of lines from a text document

**URL:** <https://www.ruby-forum.com/t/finding-a-range-of-lines-from-a-text-document/157339>\
**Category:** Ruby\
**Created:** [January 9, 2009, 4:56pm UTC](https://www.ruby-forum.com/t/finding-a-range-of-lines-from-a-text-document/157339 "2009-01-09T16:56:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [January 9, 2009, 4:56pm UTC](https://www.ruby-forum.com/t/finding-a-range-of-lines-from-a-text-document/157339/1 "2009-01-09T16:56:31Z")

</div>

Can anyone help me? ill try and keep it short and to the point.

wish : to be able to grab select lines from a text document which meet  
some criteria.

current idea: simply read the file as one big string and use a regexp to  
find all text between a /marker\_one/ and /marker\_two/

but: in general ill only be ever manipulating a single line at a time so  
simply using readlines to store a file into an array and they  
manipulating that seems a lot easier than storing a file as a string and  
figuring out where one line starts and one line ends.

file example:

### next actions

### project: buy pc

3 read posts on hardforum  
4 use [froogle.com](http://froogle.com)  
3 buy parts  
4 build pc  
5 install os

### project: become millionaire

2 get job  
3 save money  
4 start own business  
3 sell business  
3 sell drugs

### project: have the biggest collection of pants on earth

3 learn about pants  
4 become a member of [pantsinc.net](http://pantsinc.net)

aim: to have a method which will take the top most line of each project  
and copy it into the next actions area. Something easy to read yet  
relatively efficient.

yes this is yet another todo list. A task will only ever be one line.  
Hence why dealing with an array seems easier.

If anyone has any tips suggestions etc it will help me out a lot.

---

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [January 9, 2009, 6:31pm UTC](https://www.ruby-forum.com/t/finding-a-range-of-lines-from-a-text-document/157339/2 "2009-01-09T18:31:31Z")

</div>

Adam A. wrote:

> Can anyone help me? ill try and keep it short and to the point.
> 
> wish : to be able to grab select lines from a text document which meet  
> some criteria.
> 
> current idea: simply read the file as one big string and use a regexp to  
> find all text between a /marker\_one/ and /marker\_two/
> 
> but: in general ill only be ever manipulating a single line at a time so  
> simply using readlines to store a file into an array and they  
> manipulating that seems a lot easier than storing a file as a string and  
> figuring out where one line starts and one line ends.
> 
> file example:
> 
> ### next actions
> 
> ### project: buy pc
> 
> 3 read posts on hardforum  
> 4 use [froogle.com](http://froogle.com)  
> 3 buy parts  
> 4 build pc  
> 5 install os
> 
> ### project: become millionaire
> 
> 2 get job  
> 3 save money  
> 4 start own business  
> 3 sell business  
> 3 sell drugs
> 
> ### project: have the biggest collection of pants on earth
> 
> 3 learn about pants  
> 4 become a member of [pantsinc.net](http://pantsinc.net)
> 
> aim: to have a method which will take the top most line of each project  
> and copy it into the next actions area. Something easy to read yet  
> relatively efficient.
> 
> yes this is yet another todo list. A task will only ever be one line.  
> Hence why dealing with an array seems easier.
> 
> If anyone has any tips suggestions etc it will help me out a lot.

This one of the algorithms you will find along the programmer way.  
I am not going to give you the hole solution …but here is a clue…

read line by line  
once you find the starting tag turn on a flag  
do whatever you want with the current line if the flag is on  
once you find the ending tag turn off the flag

-r.

---

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [January 9, 2009, 8:21pm UTC](https://www.ruby-forum.com/t/finding-a-range-of-lines-from-a-text-document/157339/3 "2009-01-09T20:21:46Z")

</div>

Rodrigo B. wrote:

> Adam A. wrote:
> 
> > Can anyone help me? ill try and keep it short and to the point.
> > 
> > wish : to be able to grab select lines from a text document which meet  
> > some criteria.
> > 
> > current idea: simply read the file as one big string and use a regexp to  
> > find all text between a /marker\_one/ and /marker\_two/
> > 
> > but: in general ill only be ever manipulating a single line at a time so  
> > simply using readlines to store a file into an array and they  
> > manipulating that seems a lot easier than storing a file as a string and  
> > figuring out where one line starts and one line ends.
> > 
> > file example:
> > 
> > ### next actions
> > 
> > ### project: buy pc
> > 
> > 3 read posts on hardforum  
> > 4 use [froogle.com](http://froogle.com)  
> > 3 buy parts  
> > 4 build pc  
> > 5 install os
> > 
> > ### project: become millionaire
> > 
> > 2 get job  
> > 3 save money  
> > 4 start own business  
> > 3 sell business  
> > 3 sell drugs
> > 
> > ### project: have the biggest collection of pants on earth
> > 
> > 3 learn about pants  
> > 4 become a member of [pantsinc.net](http://pantsinc.net)

a better solution will be to have your project data in a  
“self-descriptive” format like XML (or YAML)

sooner or later you will have to use XML …  
it’s a good chance for you to learn it and added it to your toolbox

-r.

---

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [January 10, 2009, 4:21am UTC](https://www.ruby-forum.com/t/finding-a-range-of-lines-from-a-text-document/157339/4 "2009-01-10T04:21:33Z")

</div>

thank you for the algorithm prompt. When i read your suggestion I though  
why didnt i think of that (-perhaps too much red wine at the time).

Ive heard about xml but dont know much about it. Ill go away and learn  
about it now.

Thank you very much!
