The Accidental Rubyist

invalid byte sequence in UTF-8

A ONE-line signature rotator

leave a comment »

If you have a file with quotations, verses or whatever that you would like to print out like the fortune program or rotator, you need do only the following:

1. break the file in to multiple files, one for each quote, or verse.

perl -pe ‘BEGIN {$n=1} printf ” — Author’s name\n” and open STDOUT, “>$ARGV.$n” and $n++ if /^ \d/’ verses.txt

The above line creates files named verses.txt.1 … till as many quotes as there are. The split is based on the regex if /^ \d/ which in my case means a line starting with three spaces and then a number. You will modify this as per your file. Also at the end of each file, an attribution is printed.

2. This line prints a random quote:
cat verses.txt.`jot -r 1 1 298`

jot is a wonderful command that can print random or sequential numbers (and a lot more). This command prints 1 random number between 1 and 298. Thus a random file name is generated.

The same could be accomplished in your favorite programming language (aka ruby) as:
system(“echo verses.txt.%s” % rand(298))
(I know my ruby is poor! … but that’s just one line!)

You can dump that one line into a file, say “”, make it executable with “chmod +x“, put it in your path, and shoot verses off.

Cheap, simple, and free!

(The perl snippet is thanks to this page.)

15. There is no distinction between pleasure and pain, man and woman,
success and failure for the wise man who looks on everything as equal.
— Ashtavakra Gita


Written by totalrecall

March 16, 2007 at 5:25 am

Posted in unix

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: