From: Andrew Timberlake Date: 2009-01-13T04:33:13+09:00 Subject: Re: Data stream variables ------=_Part_336577_28370354.1231788835406 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline On Mon, Jan 12, 2009 at 9:14 PM, Robert Klemme 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://ramblingsonrails.com http://www.linkedin.com/in/andrewtimberlake "I have never let my schooling interfere with my education" - Mark Twain ------=_Part_336577_28370354.1231788835406--