tr - translate or delete specific characters
Last revision July 20, 2004
The tr utility allows you to change specific characters in a file to other characters, or to delete specific characters. It is commonly used to change or delete control characters.
reads only from the standard input and writes only to the standard output;
it will not accept file names on the command line.
Therefore, to use it with disk files, simply redirect the input and
output to files, for example,
tr searchstring replacestring < infile > outfile
The arguments to tr are two strings of characters (enclose in quotes if they contain any shell meta-characters or embedded blanks or tabs). The standard input is copied to the standard output; any character in the first "search" string that is found in the input is changed to the corresponding character in the second "replacement" string in the output. If the second, or "replacement", string of characters contains fewer characters than the first, or "search" string, it is padded out to the length of the first string by repeating its last character.
The items in either the first or second string argument may be any combination of the following:
- Simple characters.
- The octal ASCII code(s) for a character(s), specified as a backslash followed by the three digit code (for example, the code for the newline, which is "\012"). If you include any octal codes, the string must be enclosed in quotes to prevent interpretation of the backslash by the shell.
- A range of characters in ASCII collating order consisting of the first and last characters separated by a hyphen (for example, a-z).
If you use any blank or tab characters, any characters that may have special
meanings to the shell, or any octal codes, be sure to enclose the entire argument
in single quotes (apostraphes). To see the octal codes for each ASCII character,
or to see the ASCII collating order, run this command on pangea:
- Change all occurrences of one letter to another:
tr b B < infile > outfile
This will copy infile to outfile, changing all occurrences of the letter b to B.
- You can specify a range of characters in ASCII collating order with a hyphen.
tr a-z A-Z < infile > outfile
Copies infile to outfile, changing all lower case letters to their upper case equivalents.
- The -d option to tr deletes any characters from
the first "search" string (the second "replacement" string is not given). Example
to delete all "non-printing" control characters, except the tab and newline (line
tr -d '\001-\010\013-\037\177-\377' < infile > outfile
- The -c option to tr "complements" the first string.
That is, any character that is not in the first "search" string
will be changed to the second "replacement" string (which should be one character
long). Example to change all non-printing control characters, except newline,
tr -c '\012\040-\176' ' ' < infile > outfile
tr examples for converting text files from Mac to Unix or DOS/Windows to Unix:
- Documents made on a Macintosh use the <CR> character (carriage return)
as the line separator. To read those with Unix programs, you should convert to
<LF> (newline). If the file transfer program does not do the conversion
automatically (e.g., ftp in "ascii" mode), it can be done with this tr
command, using the octal representation of the two special characters:
tr '\015' '\012' < macinput > Unixoutput
- ASCII files transferred from DOS/Windows systems to Unix have both the <CR>
and <LF> characters at the end of each line and have a CTRL-Z at the end
of the file as a file termination character. The extra <CR> characters and
CTRL-Z have no meaning in Unix programs. They can be eliminated with this example,
again using the octal representation:
tr -d '\015\032' < dosinput > Unixoutput