[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Data stream variables

Alain Helfenstein

1/12/2009 1:21:00 PM

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.
--
Posted via http://www.ruby-....

6 Answers

Andrew Timberlake

1/12/2009 1:38:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

On Mon, Jan 12, 2009 at 3:21 PM, Alain Helfenstein <a-helfenstein@bluewin.ch
> wrote:

> 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.
> --
> Posted via http://www.ruby-....
>
>
Does it all have to be done in ruby?
Why not
ls *.xml | xargs ruby -n -e 'puts ($FILENAME + " " + $..to_s + "" + $_) if
/Open/'

--
Andrew Timberlake
http://ramblingso...
http://www.linkedin.com/in/andrew...

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

Alain Helfenstein

1/12/2009 2:46:00 PM

0

Andrew Timberlake wrote:
> On Mon, Jan 12, 2009 at 3:21 PM, Alain Helfenstein
> <a-helfenstein@bluewin.ch
>> 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-....
>>
>>
> Does it all have to be done in ruby?
> Why not
> ls *.xml | xargs ruby -n -e 'puts ($FILENAME + " " + $..to_s + "" + $_)
> if
> /Open/'
>
> --
> Andrew Timberlake
> http://ramblingso...
> http://www.linkedin.com/in/andrew...
>
> "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
--
Posted via http://www.ruby-....

Andrew Timberlake

1/12/2009 3:28:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

On Mon, Jan 12, 2009 at 4:45 PM, Alain Helfenstein <a-helfenstein@bluewin.ch
> wrote:

> Andrew Timberlake wrote:
> > On Mon, Jan 12, 2009 at 3:21 PM, Alain Helfenstein
> > <a-helfenstein@bluewin.ch
> >> 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-....
> >>
> >>
> > Does it all have to be done in ruby?
> > Why not
> > ls *.xml | xargs ruby -n -e 'puts ($FILENAME + " " + $..to_s + "" + $_)
> > if
> > /Open/'
> >
> > --
> > Andrew Timberlake
> > http://ramblingso...
> > http://www.linkedin.com/in/andrew...
> >
> > "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
> --
> Posted via http://www.ruby-....
>
>
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 <filename> *.xml

Hopefully that helps

--
Andrew Timberlake
http://ramblingso...
http://www.linkedin.com/in/andrew...

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

Robert Klemme

1/12/2009 7:12:00 PM

0

On 12.01.2009 14:21, Alain Helfenstein wrote:
> 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.

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

--
remember.guy do |as, often| as.you_can - without end

Andrew Timberlake

1/12/2009 7:33:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

On Mon, Jan 12, 2009 at 9:14 PM, Robert Klemme
<shortcutter@googlemail.com>wrote:

> On 12.01.2009 14:21, Alain Helfenstein wrote:
>
>> 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.
>>
>
> 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
>
> --
> remember.guy do |as, often| as.you_can - without end
>
>
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 Timberlake
http://ramblingso...
http://www.linkedin.com/in/andrew...

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

Robert Klemme

1/12/2009 7:45:00 PM

0

On 12.01.2009 20:33, Andrew Timberlake wrote:
> [Note: parts of this message were removed to make it a legal post.]
>
> On Mon, Jan 12, 2009 at 9:14 PM, Robert Klemme
> <shortcutter@googlemail.com>wrote:
>
>> On 12.01.2009 14:21, Alain Helfenstein wrote:
>>
>>> 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.
>>>
>> 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

--
remember.guy do |as, often| as.you_can - without end