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.

tr 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:

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:
      man ascii

Examples:

tr examples for converting text files from Mac to Unix or DOS/Windows to Unix: