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

On Apr 27, 1:39 am, John J. [email protected]
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 J.

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

another way, but maybe less “ruby way” and more “perl way” :

ruby -ne ‘print “#{$.} : #{$_}”’ file

On 27.04.2007 08:39, John J. 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

On 4/27/07, John J. [email protected] 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 J.

ARGF.readlines.each_with_index{
| line, idx |
puts “%3d %s” % [ idx.succ, line ]
}
HTH
Robert

On Apr 27, 3:27 am, come [email protected] 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 $…

On 4/28/07, Robert D. [email protected] wrote:

love,
John J.

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

On 28.04.2007 09:06, Robert D. wrote:

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

On 28.04.2007 15:27, John J. wrote:

| line, idx |
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

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

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.

On 4/28/07, John J. [email protected] wrote:

IO stuff.
Wait till you feel the need for map_with_index :slight_smile:

martin

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

puts “%3d %s” % [ idx.succ, line ]
Kind regards

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 4:49 AM, James Edward G. II wrote:

See the standard generator library for doing this without a counter.
You just made up that name right?
You mean try to use some more usual approach. To rethink.

On Apr 28, 2007, at 12:05 PM, John J. 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?!

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

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

require “generator”
=> “one”

James Edward G. II

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 28, 2007, at 7:11 PM, John J. wrote:

On Apr 29, 2007, at 4:49 AM, James Edward G. 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:[email protected]/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 G. II

John J. [email protected] 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).

On 28.04.2007 19:05, John J. 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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs