# Can one use line addresses...to select a portion of file

**URL:** <https://www.ruby-forum.com/t/can-one-use-line-addresses-to-select-a-portion-of-file/78188>\
**Category:** Ruby\
**Created:** [December 3, 2006, 7:21pm UTC](https://www.ruby-forum.com/t/can-one-use-line-addresses-to-select-a-portion-of-file/78188 "2006-12-03T19:21:11Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![JacobS\_Raymond\_A\_Jr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/898d66/32.png) [@JacobS\_Raymond\_A\_Jr](https://www.ruby-forum.com/u/JacobS_Raymond_A_Jr)\
**Post date:** [December 3, 2006, 7:21pm UTC](https://www.ruby-forum.com/t/can-one-use-line-addresses-to-select-a-portion-of-file/78188/1 "2006-12-03T19:21:11Z")

</div>

I would like to select a portion of a file to process like I did with  
sed.

i.e. a file called Menu with the following contents:  
**McDonalds**

…  
…  
…(stuff deleted)

_Happy\_Meal_\_

With sed I would do something like

sed -n “/ **McDonalds** /,/\__Happy\_Meal_/p” Menu

I can not find anything comparable in Ruby. I may be using the wrong  
words when I searched google for ruby line address.  
Any help will be appreciated.

Thank you,  
Raymond

---

<div class="post-metadata">

**Author:** ![JacobS\_Raymond\_A\_Jr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/898d66/32.png) [@JacobS\_Raymond\_A\_Jr](https://www.ruby-forum.com/u/JacobS_Raymond_A_Jr)\
**Post date:** [December 3, 2006, 7:43pm UTC](https://www.ruby-forum.com/t/can-one-use-line-addresses-to-select-a-portion-of-file/78188/2 "2006-12-03T19:43:14Z")

</div>

Jacob, Raymond A Jr wrote:

> _Happy\_Meal_\_  
> Raymond

For a trivial approach, and the most terse one:

lines = file.to\_a  
lines\_to\_process = lines[(lines.index(“ **McDonalds** \n”)) …  
lines.index(“ **Happy\_Meal** \n”))]

Requires loading the whole file into memory, can only, and several  
passes through it, so the performance is probably ugly, can only handle  
lines, and can’t cope with regular expressions (which is why you need  
the \n in the delimiters). But probably Good Enough for some  
applications.

A more involved solution would probably be one yielding lines between  
lines that match the delimiters to a block or returning an array of them  
if no block was given. The most flexible one would be “faking” an IO  
object that would start at the starting pattern and then look for the  
ending one as it goes, but unless you need the data without line breaks  
and without the memory overhead of keeping them in memory, probably too  
much bother.

David V.

---

<div class="post-metadata">

**Author:** ![JacobS\_Raymond\_A\_Jr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/898d66/32.png) [@JacobS\_Raymond\_A\_Jr](https://www.ruby-forum.com/u/JacobS_Raymond_A_Jr)\
**Post date:** [December 3, 2006, 7:51pm UTC](https://www.ruby-forum.com/t/can-one-use-line-addresses-to-select-a-portion-of-file/78188/3 "2006-12-03T19:51:16Z")

</div>

On Mon, Dec 04, 2006 at 03:19:25AM +0900, Jacob, Raymond A Jr wrote:  
} I would like to select a portion of a file to process like I did with  
} sed.  
}  
} i.e. a file called Menu with the following contents:  
} **McDonalds**  
}  
} …  
} …  
} …(stuff deleted)  
}  
} **Happy\_Meal**  
}  
} With sed I would do something like  
}  
} sed -n “/ **McDonalds** /,/ **Happy\_Meal** /p” Menu  
}  
} I can not find anything comparable in Ruby. I may be using the wrong  
} words when I searched google for ruby line address.  
} Any help will be appreciated.

Well, sometimes sed (or awk, which does the same thing even more simply:  
awk ‘/ **McDonalds** /,/ **Happy\_Meal** /’) is the right tool. If you are  
selected this subset of lines for further processing in ruby, however  
try  
this:

inrange = false  
lines = open(file).read.split("\n").select { |line|  
inrange &&= not / **Happy\_Meal** / === line  
inrange ||= / **McDonalds** / === line  
}

} Thank you,  
} Raymond  
–Greg

---

<div class="post-metadata">

**Author:** ![JacobS\_Raymond\_A\_Jr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/898d66/32.png) [@JacobS\_Raymond\_A\_Jr](https://www.ruby-forum.com/u/JacobS_Raymond_A_Jr)\
**Post date:** [December 4, 2006, 4:38am UTC](https://www.ruby-forum.com/t/can-one-use-line-addresses-to-select-a-portion-of-file/78188/4 "2006-12-04T04:38:17Z")

</div>

Gregory S. wrote:

> inrange = false  
> lines = open(file).read.split("\n").select { |line|  
> inrange &&= not / **Happy\_Meal** / === line  
> inrange ||= / **McDonalds** / === line  
> }  
> There is a lesser-known (and usually frowned upon?) feature in Ruby that  
> treats Range notation differently in a “boolean context.”

IO.foreach(file) do |line| # use this rather that loading the whole file  
if line =~ / **McDonalds** / … line =~ / **Happy\_Meal** /  
do\_stuff\_with line  
end  
end

Devin

---

<div class="post-metadata">

**Author:** ![JacobS\_Raymond\_A\_Jr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/898d66/32.png) [@JacobS\_Raymond\_A\_Jr](https://www.ruby-forum.com/u/JacobS_Raymond_A_Jr)\
**Post date:** [December 5, 2006, 11:10pm UTC](https://www.ruby-forum.com/t/can-one-use-line-addresses-to-select-a-portion-of-file/78188/5 "2006-12-05T23:10:53Z")

</div>

Devin M. wrote:

> if line =~ / **McDonalds** / … line =~ / **Happy\_Meal** /  
> do\_stuff\_with line  
> end  
> end

So -this- is what that does?

Does this do that?

Does anyone know what that notation does?

David V.  
Incoherent

---

<div class="post-metadata">

**Author:** ![JacobS\_Raymond\_A\_Jr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/898d66/32.png) [@JacobS\_Raymond\_A\_Jr](https://www.ruby-forum.com/u/JacobS_Raymond_A_Jr)\
**Post date:** [December 3, 2006, 8:35pm UTC](https://www.ruby-forum.com/t/can-one-use-line-addresses-to-select-a-portion-of-file/78188/6 "2006-12-03T20:35:23Z")

</div>

To: Gregory S. ] and David V.  
Thank you,  
Raymond

---

<div class="post-metadata">

**Author:** ![JacobS\_Raymond\_A\_Jr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/898d66/32.png) [@JacobS\_Raymond\_A\_Jr](https://www.ruby-forum.com/u/JacobS_Raymond_A_Jr)\
**Post date:** [December 5, 2006, 11:22pm UTC](https://www.ruby-forum.com/t/can-one-use-line-addresses-to-select-a-portion-of-file/78188/7 "2006-12-05T23:22:58Z")

</div>

On Wed, 6 Dec 2006, David V. wrote:

> So -this- is what that does?
> 
> Does this do that?

yes.

> Does anyone know what that notation does?

it’s a boolean toggle.

well doccumented in pickaxe and every perl book out there - if not  
somewhat  
esoteric.

cheers.

-a
