Output A File w/ Line Numbers?

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

···

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

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

···

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.

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

···

On 4/27/07, John Joyce <dangerwillrobinsondanger@gmail.com> wrote:

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

John Joyce <dangerwillrobinsondanger@gmail.com> writes:

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.

Also, nl(1).

···

love,
John Joyce

--
Christian Neukirchen <chneukirchen@gmail.com> http://chneukirchen.org

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

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

···

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

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

I forgot about $..

···

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

:slight_smile: 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

···

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!

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.

···

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!

:slight_smile: 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

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

···

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!

:slight_smile: 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.

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.

···

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!

:slight_smile: 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

Wait till you feel the need for map_with_index :slight_smile:

martin

···

On 4/28/07, John Joyce <dangerwillrobinsondanger@gmail.com> wrote:

>
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.

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?!

Right.

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.

Just be careful. In my experience the need for and index is often a hint that you haven't yet found the Ruby way to approach the problem. That's not always true, but often.

Where I had been building iterating blocks with some externally initialized counter.

See the standard generator library for doing this without a counter.

James Edward Gray II

···

On Apr 28, 2007, at 12:05 PM, John Joyce wrote:

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?!

Correct.

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.

:-))

I suggest you take a very close look at Enumerable because that is one of the most often used pieces of Ruby and it's at the core of all the nice iterating functionality.

Then look into Enumerator which brings this functionality to all sorts of objects (if they provide any iterating method).

Finally, check out #inject - my personal favorite.

Kind regards

  robert

···

On 28.04.2007 19:05, John Joyce wrote:

You just made up that name right?
You mean try to use some more usual approach. To rethink.

···

On Apr 29, 2007, at 4:49 AM, James Edward Gray II wrote:

See the standard generator library for doing this without a counter.

See the standard generator library for doing this without a counter.

You just made up that name right?

Nope:

>> require "generator"
=> true
>> enum = %w[one two three]
=> ["one", "two", "three"]
>> gen = Generator.new(enum)
=> #<Generator:0x14be12c @cont_endp=nil, @cont_yield=#<Continuation:0x14be050>, cont_nextnil, queue["one"], block#<Proc:0x014c3640@/usr/local/lib/ruby/1.8/generator.rb:71>, index0
>> gen.next?
=> true
>> gen.next
=> "one"
>> gen.next?
=> true
>> gen.next
=> "two"
>> gen.next?
=> true
>> gen.next
=> "three"
>> gen.next?
=> false

James Edward Gray II

···

On Apr 28, 2007, at 7:11 PM, John Joyce wrote:

On Apr 29, 2007, at 4:49 AM, James Edward Gray II wrote:

Sure enough it's in the Ruby Standard LIbrary, among the things the pickaxe glances over at the end....
I'll have to dig through that. Very interesting, but could lean towards more C++/Java-ish looking code perhaps.
Nonetheless, 1000 thanks to all.

···

On Apr 29, 2007, at 9:47 AM, James Edward Gray II wrote:

On Apr 28, 2007, at 7:11 PM, John Joyce wrote:

On Apr 29, 2007, at 4:49 AM, James Edward Gray II wrote:

See the standard generator library for doing this without a counter.

You just made up that name right?

Nope:

>> require "generator"
=> true
>> enum = %w[one two three]
=> ["one", "two", "three"]
>> gen = Generator.new(enum)
=> #<Generator:0x14be12c @cont_endp=nil, @cont_yield=#<Continuation:0x14be050>, cont_nextnil, queue["one"], block#<Proc:0x014c3640@/usr/local/lib/ruby/1.8/generator.rb:71>, index0
>> gen.next?
=> true
>> gen.next
=> "one"
>> gen.next?
=> true
>> gen.next
=> "two"
>> gen.next?
=> true
>> gen.next
=> "three"
>> gen.next?
=> false

James Edward Gray II