Changing with tr
Sometimes you just have to make changes to a file to change all occurrences of one term or character to another. For example, you might have reversed the case in a file (by accidentally typing with Caps Lock on...argh!) and need to change it back. Or you might want to turn a document into a list of words (one per line) that you can sort or count. The tr utility is just what you need (see Code Listing 6.20).
Code Listing 6.20. Use tr to translate characters in files.
[jdoe@frazz jdoe]$ cat limerick There once was a man from Nantucket, Who carried his lunch in a bucket, Said he with a sigh, As he ate a whole pie, If I just had a donut I'd dunk it. [jdoe@frazz jdoe]$ cat limerick | tr a-zA-Z A-Za-z tHERE ONCE WAS A MAN FROM nANTUCKET, wHO CARRIED HIS LUNCH IN A BUCKET, sAID HE WITH A SIGH, aS HE ATE A WHOLE PIE, iF i JUST HAD A DONUT i'D DUNK IT. [jdoe@frazz jdoe]$ cat limerick | tr -c a-zA-Z "\n There once was a man from Nantucket Who carried his lunch in a bucket Said he with a sigh As he ate a whole pie If I just had a donut I d dunk it
To Translate Case with TR:
cat limerick | tr a-zA-Z A-Za-z
At the shell prompt, use the cat command and the pipe to send a file to tr, which will then translate lowercase to uppercase, and vice versa.
To Break Lines with tr:
cat limerick | tr c a-zA-Z "\n"
Change anything that's not a letter (upper or lowercase) to a new line, thus breaking the limerick into a list of words. The c indicates that anything that does not match the first set of characters (the complement of those characters) should be changed to the new character.
Rather than use cat to send the file to tr, you can use spiffy Unix redirection tools (< in this case) to do it. An equivalent command to translate case would be tr a-zA-Z A-Za-z < limerick
With tr, you can accomplish all kinds of translations. For example, you could set up a bit of a code to keep secret information somewhat secret, by translating letters to garble your text, then retranslating when you want them. For example, use cat limerick | tr a-mA-Mn-zN-Z n-zN-Za-mA-M > limerick.rot13 to encode and cat limerick.rot13 | tr n-zN-ZamA-M a-mA-Mn-zN-Z to decode. This is the same as ROT13, discussed in Chapter 17, but far more flexible and spiffy if you use TR to do it.
Check out the man page for TR (man tr) for details on the other cool translations and conversions it can do.