# Data stream variables

**URL:** <https://www.ruby-forum.com/t/data-stream-variables/157510>\
**Category:** Ruby\
**Created:** [January 12, 2009, 2:22pm UTC](https://www.ruby-forum.com/t/data-stream-variables/157510 "2009-01-12T14:22:01Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![helpy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/d2c977/32.png) [@helpy](https://www.ruby-forum.com/u/helpy)\
**Post date:** [January 12, 2009, 2:22pm UTC](https://www.ruby-forum.com/t/data-stream-variables/157510/1 "2009-01-12T14:22:01Z")

</div>

Hi

I want to print all lines wihtch contains the string “Open” from a bunch  
of ‘xml’ files. Additionally I want to print the filename and the line  
number of the match.

The problem with the following snippet is, that the variable ‘$.’ does  
not reset the line number at the beginning of a new file.

That menas, the printed file numbers are not correct except for the  
first file of ‘\*.xml’

ruby -n -e ‘puts ($FILENAME + " " + $…to\_s + “” + $\_) if /Open/’ \*.xml

Does anybody have a idea, on how to print the correct line number?

Thanks a lot for your answer, Alain.

---

<div class="post-metadata">

**Author:** ![helpy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/d2c977/32.png) [@helpy](https://www.ruby-forum.com/u/helpy)\
**Post date:** [January 12, 2009, 2:39pm UTC](https://www.ruby-forum.com/t/data-stream-variables/157510/2 "2009-01-12T14:39:08Z")

</div>

On Mon, Jan 12, 2009 at 3:21 PM, Alain H.  
\<removed\_email\_address@domain.invalid

> wrote:

> first file of ‘\*.xml’
> 
> ruby -n -e ‘puts ($FILENAME + " " + $…to\_s + “” + $\_) if /Open/’ \*.xml
> 
> Does anybody have a idea, on how to print the correct line number?
> 
> ## Thanks a lot for your answer, Alain.
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Does it all have to be done in ruby?  
Why not  
ls \*.xml | xargs ruby -n -e ‘puts ($FILENAME + " " + $…to\_s + “” + $\_)  
if  
/Open/’

–  
Andrew T.  
[http://ramblingsonrails.com](http://ramblingsonrails.com)  
[http://www.linkedin.com/in/andrewtimberlake](http://www.linkedin.com/in/andrewtimberlake)

“I have never let my schooling interfere with my education” - Mark Twain

---

<div class="post-metadata">

**Author:** ![helpy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/d2c977/32.png) [@helpy](https://www.ruby-forum.com/u/helpy)\
**Post date:** [January 12, 2009, 3:46pm UTC](https://www.ruby-forum.com/t/data-stream-variables/157510/3 "2009-01-12T15:46:18Z")

</div>

Andrew T. wrote:

> On Mon, Jan 12, 2009 at 3:21 PM, Alain H.  
> \<removed\_email\_address@domain.invalid
> 
> > wrote:
> 
> > first file of ‘\*.xml’
> > 
> > ruby -n -e ‘puts ($FILENAME + " " + $…to\_s + “” + $\_) if /Open/’ \*.xml
> > 
> > Does anybody have a idea, on how to print the correct line number?
> > 
> > ## Thanks a lot for your answer, Alain.
> > 
> > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> Does it all have to be done in ruby?  
> Why not  
> ls \*.xml | xargs ruby -n -e ‘puts ($FILENAME + " " + $…to\_s + “” + $\_)  
> if  
> /Open/’
> 
> –  
> Andrew T.  
> [http://ramblingsonrails.com](http://ramblingsonrails.com)  
> [http://www.linkedin.com/in/andrewtimberlake](http://www.linkedin.com/in/andrewtimberlake)
> 
> “I have never let my schooling interfere with my education” - Mark Twain

Thanks a lot for your answer.

Sadly I’m running Windows XP.

But I try to get GnuWin32 running…

Alain

---

<div class="post-metadata">

**Author:** ![helpy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/d2c977/32.png) [@helpy](https://www.ruby-forum.com/u/helpy)\
**Post date:** [January 12, 2009, 8:15pm UTC](https://www.ruby-forum.com/t/data-stream-variables/157510/4 "2009-01-12T20:15:50Z")

</div>

On 12.01.2009 14:21, Alain H. wrote:

> ruby -n -e ‘puts ($FILENAME + " " + $…to\_s + “” + $\_) if /Open/’ \*.xml
> 
> Does anybody have a idea, on how to print the correct line number?
> 
> Thanks a lot for your answer, Alain.

I’m not Alain, but… 🙂 Just kidding.

Here’s how to do it:

ruby -ne ‘puts “#$FILENAME #$. #$\_” if /Open/; $.=0 if ARGF.eof?’ \*.xml

Cheers

robert

---

<div class="post-metadata">

**Author:** ![helpy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/d2c977/32.png) [@helpy](https://www.ruby-forum.com/u/helpy)\
**Post date:** [January 12, 2009, 8:34pm UTC](https://www.ruby-forum.com/t/data-stream-variables/157510/5 "2009-01-12T20:34:43Z")

</div>

On Mon, Jan 12, 2009 at 9:14 PM, Robert K.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> > first file of ‘\*.xml’  
> > Here’s how to do it:

I was wondering if you could reset the line counter but didn’t think of  
just  
setting $. = 0  
Sometimes ruby is just too easy 🙂 I still sometimes think things need  
to  
be more complicated.

–  
Andrew T.  
[http://ramblingsonrails.com](http://ramblingsonrails.com)  
[http://www.linkedin.com/in/andrewtimberlake](http://www.linkedin.com/in/andrewtimberlake)

“I have never let my schooling interfere with my education” - Mark Twain

---

<div class="post-metadata">

**Author:** ![helpy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/d2c977/32.png) [@helpy](https://www.ruby-forum.com/u/helpy)\
**Post date:** [January 12, 2009, 4:29pm UTC](https://www.ruby-forum.com/t/data-stream-variables/157510/6 "2009-01-12T16:29:47Z")

</div>

On Mon, Jan 12, 2009 at 4:45 PM, Alain H.  
\<removed\_email\_address@domain.invalid

> wrote:

> > >
> 
> But I try to get GnuWin32 running…
> 
> ## Alain
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Alain

Sorry, I made a mistake with my answer above. Instead of using xargs  
(which  
was exactly the same as your method - actually)  
Use find to make sure ruby gets a unique file each time:  
find \*.xml -exec ruby -n -e ‘puts ($FILENAME + " " + $…to\_s + “” + $\_)  
if  
/Open/’ {} ;  
–still doesn’t help on Windows though

Instead of trying to do this in a one-liner, save the following as a  
file:  
ARGV.each do |filename|  
File.open(File.join(File.dirname( **FILE** ), filename)) do |file|  
while line = file.gets  
puts (filename + " " + $…to\_s + “” + $\_) if line =~ /Open/  
end  
end  
end

And then run:  
ruby \*.xml

Hopefully that helps

–  
Andrew T.  
[http://ramblingsonrails.com](http://ramblingsonrails.com)  
[http://www.linkedin.com/in/andrewtimberlake](http://www.linkedin.com/in/andrewtimberlake)

“I have never let my schooling interfere with my education” - Mark Twain

---

<div class="post-metadata">

**Author:** ![helpy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/d2c977/32.png) [@helpy](https://www.ruby-forum.com/u/helpy)\
**Post date:** [January 12, 2009, 8:45pm UTC](https://www.ruby-forum.com/t/data-stream-variables/157510/7 "2009-01-12T20:45:54Z")

</div>

On 12.01.2009 20:33, Andrew T. wrote:

> > > Thanks a lot for your answer, Alain.
> > 
> > I’m not Alain, but… 🙂 Just kidding.
> > 
> > Here’s how to do it:
> > 
> > ruby -ne ‘puts “#$FILENAME #$. #$\_” if /Open/; $.=0 if ARGF.eof?’ \*.xml
> 
> I was wondering if you could reset the line counter but didn’t think of just  
> setting $. = 0

IMHO the tricky bit is to know _when_ to reset, i.e. to know that  
ARGF#eof? signals the end of a single file and not all files. 🙂

> Sometimes ruby is just too easy 🙂 I still sometimes think things need to  
> be more complicated.

🙂 The other area where I simplified your piece of code is the string  
handling. Btw, when String interpolation (i.e. #$GLOBAL or #{expr}) you  
do not need the explicit to\_s.

Kind regards

robert
