Specifications

30 | august 2008 www.linuxjournal.com
Last month, we had a lot of fun digging around
within the Internet Movie Database, producing a set
of scripts that together make it easy to generate a
list of the top 250 movies on the site with release
dates. The format of the output is:
All About Eve | 1950
Hotel Rwanda | 2004
Sin City | 2005
City Lights | 1931
This month, I take a look at how you can break
those two fields up and randomly generate some
likely release dates close to the actual date, then
send it as a question on Twitter. For example, it
might ask, “Hotel Rwanda was released in: 2000,
2001, 2004 or 2007?”
Splitting Up the Fields
Okay, this should be super easy for anyone read-
ing this column. There are a bunch of ways to
take a two-field data record and split it up, but
my favorite tool for this sort of task is cut. So,
we can do this:
moviename="$(echo $entry | cut -d\| -f1)"
releasedate=$(echo $entry | cut -d\| -f2)"
That was easy, right? Now, of course, if you
want to be fancy about it, you’ll want to strip any
leading or trailing spaces too, which can be done
with this sed command:
sed 's/^ //g;s/ $//g'
But, how do you get a random line out of a
text file?
If you recall from previous columns, one of the
secret features of the Bash shell’s built-in mathemat-
ical capabilities—accessible with $(( )) notation—is
the ability to get a random integer without any
further fuss, like this:
echo $(( $RANDOM ))
Try it in your own command shell a few times,
and you’ll get a series of random integer values, like
29408 and 17501. To constrain it to the size of the
file, we could do something fancy with
wc -l
to
identify the number of lines in the actual data file,
but because we already know we’re grabbing 250
film titles from IMDb, it’s easy just to use that value.
Here’s the first stab:
pickline="$(( $RANDOM % 250 )) "
It’s not quite right though, because we’ll get
values 0–254. You can verify this by entering the
command
echo $(( 5 % 5 ))
, for example. So,
we need to shift things up one:
pickline="$(expr $(( $RANDOM % 250 )) + 1 )"
That produces a random number. To extract that
value from a file of lines, there are a number of
solutions, but I’ll stick with sed. In that case, the
solution for pulling out line 33, as an example, is:
sed -n 33p
If you change the value to a variable name,
however, there’s a problem:
sed -n $picklinep
You can’t put a space between the variable
name and the p, but if you don’t, you have a
bad variable name, because it’s pickline, not
picklinep. The solution is a secret notational
convention you can use in scripts when there’s
any sort of ambiguity like this—curly brackets.
Movie Trivia and Fun
with Random Numbers
Use the shell to manipulate a list of movies from the Internet Movie
Database (IMDb).
WORK THE SHELL
COLUMNS
The solution is a secret notational
convention you can use in scripts
when there’s any sort of ambiguity
like this—curly brackets.
DAVE TAYLOR