Unix Power ToolsUnix Power ToolsSearch this book

21.11. Hacking on Characters with tr

The tr command is a character translation filter, reading standard input (Section 43.1) and either deleting specific characters or substituting one character for another.

The most common use of tr is to change each character in one string to the corresponding character in a second string. (A string of consecutive ASCII characters can be represented as a hyphen-separated range.)

For example, the command:

< Section 43.1

$ tr 'A-Z' 'a-z' < file        Berkeley version

will convert all uppercase characters in file to the equivalent lowercase characters. The result is printed on standard output.

In fact, a frequent trick I use tr for is to convert filenames from all uppercase to all lowercase. This comes up when you're dealing with files from MS-DOS or VMS that you are copying on to a Unix filesystem. To change all the files in the current directory to uppercase, try this from a Bash or Bourne shell prompt:

$ for i in `ls`; do mv $i `echo $i | tr [A-Z] [a-z]`; done

Of course, you need to be careful that there are no files that have the same name regardless of case. The GNU mv can be passed the -i flag that will make the program prompt you before overwriting an existing file. If you want to uppercase filenames, simply flip the arguments to tr. You can even apply this to an entire branch of a file system by sticking this in a find command. First, create a small shell script that can downcase a file and call it downcase:

#!/bin/sh
mv $1 `echo $1 | tr [A-Z] [a-z]`

Now you can really do some damage with find:

$ find /directory/to/be/affected -exec 'downcase' '{}' ';'

Obviously, running this programming on random directories as root is not recomended, unless you're looking to test your backup system.

Figure Go to http://examples.oreilly.com/upt3 for more information on: tr

In the System V version of tr, square brackets must surround any range of characters. That is, you have to say [a-z] instead of simply a-z. And of course, because square brackets are meaningful to the shell, you must protect them from interpretation by putting the string in quotes. The GNU tr, on the web site, is basically the System V version.

If you aren't sure which version you have, here's a test. Both tr examples below will convert the lowercase letters a through z to an uppercase A, but that's not what we're testing here. The Berkeley version also converts the input [ ] to A characters because [ ] aren't treated as range operators:

% echo '[ ]' | tr '[a-z]' A
AA                               Berkeley version
% echo '[ ]' | tr '[a-z]' A
[ ]                               System V version

There's one place you don't have to worry about the difference between the two versions: when you're converting one range to another range, and both ranges have the same number of characters. For example, this command works in both versions:

$ tr '[A-Z]' '[a-z]' < file
       both versions

The Berkeley tr will convert a [ from the first string into the same character [ in the second string, and the same for the ] characters. The System V version uses the [ ] characters as range operators. In both versions, you get what you want: the range A-Z is converted to the corresponding range a-z. Again, this trick works only when both ranges have the same number of characters.

The System V version also has a nice feature: the syntax [a*n], where n is some digit, means that the string should consist of n repetitions of character "a." If n isn't specified or is 0, it is taken to be some indefinitely large number. This is useful if you don't know how many characters might be included in the first string.

As described in Section 17.18, this translation (and the reverse) can be useful from within vi for translating a string. You can also delete specific characters. The -d option deletes from the input each occurrence of one or more characters specified in a string (special characters should be placed within quotation marks to protect them from the shell). For instance, the following command passes to standard output the contents of file with all punctuation deleted (and is a great exercise in shell quoting (Section 27.12)):

$ tr -d ",.\!?;:\"\'`" < file

The -s (squeeze) option of tr removes multiple consecutive occurrences of the same character in the second argument. For example, the command:

$ tr -s " " " " < file 

will print on standard output a copy of file in which multiple spaces in sequence have been replaced with a single space.

We've also found tr useful when converting documents created on other systems for use under Unix. For example, as described in Section 1.8, tr can be used to change the carriage returns at the end of each line in a Macintosh text file into the newline Unix expects. tr allows you to specify characters as octal values by preceding the value with a backslash, so the following command does the trick:

$ tr '\015' '\012' < file.mac > file.unix

The command:

$  tr -d '\015' < pc.file

will remove the carriage return from the carriage return/newline pair that a PC file uses as a line terminator. (This command is also handy for removing the excess carriage returns from a file created with script (Section 37.7).)

--TOR, JP, and JJ



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.