Numbering lines with Ruby
I’ve been fiddling around a little more with integrating Ruby scripts and one-liners with Quicksilver, and I found a way to number the lines of a selection. This is handy for those times when you decide to bullet-point some text. Generating bullet numbering manually is a chore, and if you rearrange your points you need to renumber all the following points. It’s a simple and not very elegant bit of codeâI’m sure someone with more than my newbie’s knowledge of Ruby could make it much more efficient. For some reason, I couldn’t seem to cram it all into a one-liner, so you’ll have to save the code below as a file somewhere on your system.
You need to call this script via Quicksilver or the command line like this: ruby /pathto/line_nos.rb | pbcopy. Then copy your text to be numbered, run the line above, then paste. Voila! Numbered lines. Note that the backslash at the end of the line starting ‘$std_out’ indicates a continuation line.
#!/usr/bin/env ruby
# line_nos.rb
text = `pbpaste`
lines = text.split(" n")
def number_lines(max)
i = 0
while i < max
yield i
i += 1
end
end
$std_out = number_lines(lines.length) {|val| num = val + 1;
puts "#{num}. #{lines[val]}n"}
>

1
Not knowing Ruby, I got as far as: pbpaste | awk '{printf ("m %sn",NR,$0);}' | pbcopy
before looking for a more unixy way without using any programming language... pbpaste| cat -n | pbcopy as long as you don't mind the indent being 'too big' for short files. Now I'm in the mood, it's on to my current one-liner puzzle - pick a random selection of 100 lines from a file of 50000, in a random order.
(actually, I suppose I misread your Ruby program - I thought it was being fancy and working out how much indent to use, at first)----- The �unixy way� would be using
nl, I suppose. That is also more configurable thancat -n.As for your one-liner puzzle: I think you need a little bit of
awkscripting to do this. Thus, you could give each line a random number by which yousortthe lines.by Frank @ 09/10/2004 2:11 am • Permalink •
2
nl isn't available on OSX though, unfortunately. There's a somewhat odd subset of BSD's little utilties available.
I got the following in the end, for my random playlist one-liner. CNT=
wc -l < jukebox.txtjot -r $CNT 1 $CNT | lam - -s " " jukebox.txt | sort -n | grep TRACK | head -n100 | cut -d" " -f 2-jot produces random numbers within a range. lam 'laminates' two files together, with some seperation, to put the random line numbers in. The cut at the end strips the line numbers back off to give the original lines. The extra grep is just that the file in question contains other data for my MP3 collection, like cover scans and album information, and I only wanted tracks. The funny wc < file bit is the only way I could figure to get just a line-count for a file. This seems to run a bit quicker than my previous perl-based effort at the same thing, too.
by Howard Jones @ 09/10/2004 12:10 pm • Permalink •
3
nl isn't available on OSX though, unfortunately. There's a somewhat odd subset of BSD's little utilties available.
I got the following in the end, for my random playlist one-liner. CNT=
wc -l < jukebox.txtjot -r $CNT 1 $CNT | lam - -s " " jukebox.txt | sort -n | grep TRACK | head -n100 | cut -d" " -f 2-jot produces random numbers within a range. lam 'laminates' two files together, with some seperation, to put the random line numbers in. The cut at the end strips the line numbers back off to give the original lines. The extra grep is just that the file in question contains other data for my MP3 collection, like cover scans and album information, and I only wanted tracks. The funny wc < file bit is the only way I could figure to get just a line-count for a file. This seems to run a bit quicker than my previous perl-based effort at the same thing, too.
by Howard Jones @ 09/10/2004 12:10 pm • Permalink •
4
Well, there was a point where I thought I'd learn Ruby. Then I tried, got confused, realised I couldn't do anything useful with it on Windows anyway, and stopped. Now I'd be interested in starting to try again were it not for the fact that my Powerbook is still a year and a bit away, you're turning out a load of useful stuff anyway, and Bash scripting is more useful to me at the moment. Ruby does look exceedingly useful though, as does Quicksilver...
by Matthew @ 09/10/2004 5:10 pm • Permalink •
5
Howard Jones: Heh.
And no, my code doesn't do anything fancy with indentation.
cat -nis a tad shorterYour jukebox script is really neat.
Frank: I had a look for nl before seeing Howard's comment, and realised that it wasn't installed.
Matthew: Yes, Ruby is a pretty cool language (though I'm sure that Perl and Python fans would beg to differ. I'm finding it relatively painless to learn, which must be a good thing.
by bsag @ 09/10/2004 7:10 pm • Permalink •
6
Nice Ruby-ness! I'll award you 0x01111 out of 0x100000 for geekiness. But for Macness it will only be 3 mice out of 5.
Something like this would score more:
set {lineNumber, answer} to {1, {}} repeat with i in (every paragraph of (the clipboard))     set end of answer to lineNumber & tab & i & return     set lineNumber to lineNumber + 1 end repeat set the clipboard to answer as text
(Just kidding - you wouldn't want to use AppleScript for anything complicated...
by pete @ 09/10/2004 7:10 pm • Permalink •
7
Nicely written. I can relate to most of your points.
by Dr.Death @ 27/03/2008 3:03 pm • Permalink •
Page 1 of 1 pages