A primer on using 'jot' in UNIX

Nov 17, '01 11:57:43PM

Contributed by: bsletten

I am new to Mac OS X but come from the FreeBSD world so I'm having fun finding out what is here and what isn't. I just realized that one of my favorite tools, 'jot', is here and I wanted to point this out to people. jot allows you to print sequential and random data. More specifically, it allows you to do some really cool things in the shell. Let me give you an example.

I am a big fan of the online music trading organization Etree. There are well-established ways of organizing and naming the concerts that are traded. Occasionally I'll come across a downloaded show that doesn't follow the scheme. Say there are several songs from a Phish show named something like '2001-05-26-phish-d1t1.shn'. The correct format should be 'ph01-05-26d1t1.shn' It would be a real pain in the ass to rename them, right? Well, with jot the answer is a resounding "NO!"

Read the rest of this article for a great primer on using 'jot'...

First, let's look at some basics. Open up a terminal shell and type: "jot 3 1" and hit enter. What you'll see is this:

1
2
3
What those basic options did was to generate a series of numbers (3) beginning at 1. Try something 'jot 5 2' or 'jot 10 20'. You may wonder what is so special about that. I'm going to take a monstrous leap here and jump right into some shell programming (if you aren't familiar with it, I encourage you to find some online documentations, buy a book or something). Please also note that the syntax used here is of the Bourne shell variety. If you don't have 'bash' installed, when you open up a terminal window, type 'sh' and then enter to run a Bourne shell. With modified syntax, these same examples could run under csh/tcsh.

While generating numbers themselves might not be so interesting, when fed into other commands, they can help you do quite a bit. Try this from your shell:
for i in `jot 10 1`; do echo $i; done
It looks basically like the normal jot output. But now try:
for i in `jot 10 2`; do echo "I wish I had $i dollars"; done
Notice how easy it was to integrate text and a sequence of numbers? Perhaps now you are beginning to see the power of jot. Let's look at some other features before tackling our renaming problem.

The -b option:
Say you need to generate data for a file to be read by another program. If you want the same thing to be generated over and over (like the 'yes' command if you are familiar with it), you can say:
jot -b Input 10 1 > inputfile
If you type 'cat inputfile' you'll see that the word 'Input' has been generated ten times. This is a useful way to generate files that need to be of a certain size or to test boundary conditions, buffering, etc.

The -r option:
Say you want to fill a file with random numbers (don't count on them being cryptographically secure!), you can say:
jot -r 10 1 > randomdata
now type 'cat randomdata' to see the results.

The -w option:
Finally, this option works a little like the C printf function in that it takes a formatting string. A simple substitution is to use the %d token to represent a number. Try typing:
jot -w "file%d" 10 1
You should see:
file1
file2
file3
file4
.
.
file10
If you want to specify leading zeros and two digits you would say:
jot -w "file%02d" 10 1
(n.b. leave off the zero for a space-padded number that takes up two digits).

So, let's put it all together and get back to the example. Our source files are named '2001-05-26-phish-d1t1.shn' and we want them to become 'ph01-05-26d1t1.shn'. We notice that the variance will be in the track number (t1 t2 t3) but that the mapping is otherwise the same. So, we could simply say:
for i in `jot  10 1`; do mv 2001-05-26-phish-d1t$i.shn ph01-05-26d1t$i.shn; done
What would have been cumbersome to do manually becomes a snap with this great tool. If we then want to unshorten only the first disc's files and encode the resulting .wav files as .mp3s, we could say:
for i in `jot -w "ph01-05-26d1t%d"; do shorten -x $i.shn $i.wav; rm $i.shn; bladenc -del $i.wav; done
In one swell foop, you've combined several powerful commands into a sequential and specific flow that takes only seconds to type.

Play around with jot and get used to what it can do. You'll find that it becomes a very useful tool to have on hand when you need it. If you are new to shell programming, I encourage you to exercise caution when mv'ing, rm'ing or otherwise potentially losing data. Getting the arguments wrong to a tool like jot can result in situations you did not intend.

I hope you find this tool as useful as I do. Don't forget to check out all the great music on Etree, too!

Comments (5)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20011117235743849