# Need to step through text in Variable

**URL:** <https://www.ruby-forum.com/t/need-to-step-through-text-in-variable/185250>\
**Category:** Ruby\
**Created:** [March 16, 2010, 9:24pm UTC](https://www.ruby-forum.com/t/need-to-step-through-text-in-variable/185250 "2010-03-16T21:24:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jackster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@jackster](https://www.ruby-forum.com/u/jackster)\
**Post date:** [March 16, 2010, 9:24pm UTC](https://www.ruby-forum.com/t/need-to-step-through-text-in-variable/185250/1 "2010-03-16T21:24:55Z")

</div>

My script starts out by connecting to another machine via SSH and  
reading the hosts file into a variable:

z = `ssh -l username -i /home/username/.ssh/id_rsa 10.1.1.1 cat /etc/hosts`

Normally if the hosts file was on my local machine, I would be able to  
step through the contents of the file line by line and make decisions  
based on REGEX using the file open commands.

I don’t want to have to write the contents of my variable to a file and  
then open and step through it that way.

What would be the best way to step through my z variable line by line  
and match on the content with REGEX?

thanks

jack

---

<div class="post-metadata">

**Author:** ![jackster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@jackster](https://www.ruby-forum.com/u/jackster)\
**Post date:** [March 16, 2010, 9:57pm UTC](https://www.ruby-forum.com/t/need-to-step-through-text-in-variable/185250/2 "2010-03-16T21:57:05Z")

</div>

Hello Jack

> What would be the best way to step through my z variable line by line  
> and match on the content with REGEX?

z.split(/\n/).each do |line|  
…  
end

should do what you are looking for.

Cheers,

---

<div class="post-metadata">

**Author:** ![jackster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@jackster](https://www.ruby-forum.com/u/jackster)\
**Post date:** [March 16, 2010, 10:03pm UTC](https://www.ruby-forum.com/t/need-to-step-through-text-in-variable/185250/3 "2010-03-16T22:03:32Z")

</div>

thanks alot Jean-Julien…that works perfectly!

jack

---

<div class="post-metadata">

**Author:** ![jackster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@jackster](https://www.ruby-forum.com/u/jackster)\
**Post date:** [March 17, 2010, 9:11am UTC](https://www.ruby-forum.com/t/need-to-step-through-text-in-variable/185250/4 "2010-03-17T09:11:52Z")

</div>

2010/3/16 jackster the jackle [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> thanks alot Jean-Julien…that works perfectly!

Somehow my response to the newsgroup is delayed. You do not need  
#split. You can do

`ssh ...`.each\_line do |line|  
…  
end

This works at least in 1.8.7 and 1.9, probably also in 1.8.6.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![jackster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@jackster](https://www.ruby-forum.com/u/jackster)\
**Post date:** [March 17, 2010, 4:38pm UTC](https://www.ruby-forum.com/t/need-to-step-through-text-in-variable/185250/5 "2010-03-17T16:38:36Z")

</div>

Robert K. wrote:

Better still!

thanks alot Robert
