[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Output A File w/ Line Numbers?

John Joyce

4/27/2007 6:40:00 AM

I'd like to read a file and output its contents (just to terminal is
fine for simplicity) with line numbers.
Is there a Ruby library method that handles this like unix cat?
Of course we can write a looping one-liner, but it seems to me this
function must already exist.

love,
John Joyce

16 Answers

William James

4/27/2007 7:20:00 AM

0

On Apr 27, 1:39 am, John Joyce <dangerwillrobinsondan...@gmail.com>
wrote:
> I'd like to read a file and output its contents (just to terminal is
> fine for simplicity) with line numbers.
> Is there a Ruby library method that handles this like unix cat?
> Of course we can write a looping one-liner, but it seems to me this
> function must already exist.
>
> love,
> John Joyce

ruby -ne 'BEGIN{$n=0}; print "#{$n+=1} "; print' junk

come

4/27/2007 8:28:00 AM

0

another way, but maybe less "ruby way" and more "perl way" :

ruby -ne 'print "#{$.} : #{$_}"' file


>
> ruby -ne 'BEGIN{$n=0}; print "#{$n+=1} "; print' junk


Robert Klemme

4/27/2007 9:12:00 AM

0

On 27.04.2007 08:39, John Joyce wrote:
> I'd like to read a file and output its contents (just to terminal is
> fine for simplicity) with line numbers.
> Is there a Ruby library method that handles this like unix cat?
> Of course we can write a looping one-liner, but it seems to me this
> function must already exist.

11:09:44 [~]: ruby -ne 'print $., " ",$_' foo.txt
1 aaa
2 bbbb
11:09:58 [~]: cat foo.txt
aaa
bbbb
11:10:01 [~]: cat -n foo.txt
1 aaa
2 bbbb
11:10:04 [~]:

If you want it more integrated in a script:

11:11:00 [~]: ruby -e 'ARGF.each {|line| print ARGF.lineno, " ", line}'
foo.txt
1 aaa
2 bbbb
11:11:03 [~]:

This also works with arbitrary IO's.

Kind regards

robert

William James

4/27/2007 9:25:00 AM

0

On Apr 27, 3:27 am, come <come.n...@free.fr> wrote:
> another way, but maybe less "ruby way" and more "perl way" :
>
> ruby -ne 'print "#{$.} : #{$_}"' file
>
>
>
> > ruby -ne 'BEGIN{$n=0}; print "#{$n+=1} "; print' junk

I forgot about $..

Robert Dober

4/28/2007 7:04:00 AM

0

On 4/27/07, John Joyce <dangerwillrobinsondanger@gmail.com> wrote:
> I'd like to read a file and output its contents (just to terminal is
> fine for simplicity) with line numbers.
> Is there a Ruby library method that handles this like unix cat?
> Of course we can write a looping one-liner, but it seems to me this
> function must already exist.
>
> love,
> John Joyce
>
>
ARGF.readlines.each_with_index{
| line, idx |
puts "%3d %s" % [ idx.succ, line ]
}
HTH
Robert

--
You see things; and you say Why?
But I dream things that never were; and I say Why not?
-- George Bernard Shaw

Robert Dober

4/28/2007 7:07:00 AM

0

On 4/28/07, Robert Dober <robert.dober@gmail.com> wrote:

> > love,
> > John Joyce
> >
> >
> ARGF.readlines.each_with_index{
> | line, idx |
> puts "%3d %s" % [ idx.succ, line ]
> }
> HTH
> Robert

But Robert's solution is nicer, I did not know about ARGF.lineno, and
have overlooked his solution, sorry!
R



--
You see things; and you say Why?
But I dream things that never were; and I say Why not?
-- George Bernard Shaw

Robert Klemme

4/28/2007 12:49:00 PM

0

On 28.04.2007 09:06, Robert Dober wrote:
> On 4/28/07, Robert Dober <robert.dober@gmail.com> wrote:
>
>> > love,
>> > John Joyce
>> >
>> >
>> ARGF.readlines.each_with_index{
>> | line, idx |
>> puts "%3d %s" % [ idx.succ, line ]
>> }
>> HTH
>> Robert
>
> But Robert's solution is nicer, I did not know about ARGF.lineno, and
> have overlooked his solution, sorry!

:-) Thank you! Btw, you can also do ARGF.each_with_index so you do not
have to read the whole file into mem before printing it.

Kind regards

robert


John Joyce

4/28/2007 1:28:00 PM

0


On Apr 28, 2007, at 9:50 PM, Robert Klemme wrote:

> On 28.04.2007 09:06, Robert Dober wrote:
>> On 4/28/07, Robert Dober <robert.dober@gmail.com> wrote:
>>> > love,
>>> > John Joyce
>>> >
>>> >
>>> ARGF.readlines.each_with_index{
>>> | line, idx |
>>> puts "%3d %s" % [ idx.succ, line ]
>>> }
>>> HTH
>>> Robert
>> But Robert's solution is nicer, I did not know about ARGF.lineno, and
>> have overlooked his solution, sorry!
>
> :-) Thank you! Btw, you can also do ARGF.each_with_index so you
> do not have to read the whole file into mem before printing it.
>
> Kind regards
>
> robert
This .each_with_index seems to be an invaluable method... I'd like to
know more about it. Can anyone give a little more detail on how
each_with_index works? The pickaxe covered it too briefly for my
feeble mind, but clearly it's got legs.

Robert Klemme

4/28/2007 4:08:00 PM

0

On 28.04.2007 15:27, John Joyce wrote:
>
> On Apr 28, 2007, at 9:50 PM, Robert Klemme wrote:
>
>> On 28.04.2007 09:06, Robert Dober wrote:
>>> On 4/28/07, Robert Dober <robert.dober@gmail.com> wrote:
>>>> > love,
>>>> > John Joyce
>>>> >
>>>> >
>>>> ARGF.readlines.each_with_index{
>>>> | line, idx |
>>>> puts "%3d %s" % [ idx.succ, line ]
>>>> }
>>>> HTH
>>>> Robert
>>> But Robert's solution is nicer, I did not know about ARGF.lineno, and
>>> have overlooked his solution, sorry!
>>
>> :-) Thank you! Btw, you can also do ARGF.each_with_index so you do
>> not have to read the whole file into mem before printing it.
>>
>> Kind regards
>>
>> robert
> This .each_with_index seems to be an invaluable method... I'd like to
> know more about it. Can anyone give a little more detail on how
> each_with_index works? The pickaxe covered it too briefly for my feeble
> mind, but clearly it's got legs.

There's not much to it. It's defined in module Enumerable and yields
the current element plus a count to the block. You can imagine it
implemented like this but of course it's written in C:

module Enumerable
def ewi
c=0
each {|x| yield x, c; c+=1}
end
end

Kind regards

robert

John Joyce

4/28/2007 5:06:00 PM

0


On Apr 29, 2007, at 1:10 AM, Robert Klemme wrote:

> On 28.04.2007 15:27, John Joyce wrote:
>> On Apr 28, 2007, at 9:50 PM, Robert Klemme wrote:
>>> On 28.04.2007 09:06, Robert Dober wrote:
>>>> On 4/28/07, Robert Dober <robert.dober@gmail.com> wrote:
>>>>> > love,
>>>>> > John Joyce
>>>>> >
>>>>> >
>>>>> ARGF.readlines.each_with_index{
>>>>> | line, idx |
>>>>> puts "%3d %s" % [ idx.succ, line ]
>>>>> }
>>>>> HTH
>>>>> Robert
>>>> But Robert's solution is nicer, I did not know about
>>>> ARGF.lineno, and
>>>> have overlooked his solution, sorry!
>>>
>>> :-) Thank you! Btw, you can also do ARGF.each_with_index so you
>>> do not have to read the whole file into mem before printing it.
>>>
>>> Kind regards
>>>
>>> robert
>> This .each_with_index seems to be an invaluable method... I'd like
>> to know more about it. Can anyone give a little more detail on how
>> each_with_index works? The pickaxe covered it too briefly for my
>> feeble mind, but clearly it's got legs.
>
> There's not much to it. It's defined in module Enumerable and
> yields the current element plus a count to the block. You can
> imagine it implemented like this but of course it's written in C:
>
> module Enumerable
> def ewi
> c=0
> each {|x| yield x, c; c+=1}
> end
> end
>
> Kind regards
>
> robert
>
So let me see if I got this close to straight:
each_with_index is like each in that it alows you to iterate through
the array, but additionally hands over a counter that is also
incremented?! If this is the deal, then I'm gonna have to go to rehab
because of that method. How useful it will be. I think for the last
week I've been wanting exactly that in more places than I can think
of. Where I had been building iterating blocks with some externally
initialized counter. I knew I'd missed something golden. Hadn't
looked into the Enumerator lib yet. Been forging through the File an
IO stuff.