(coreutils.info.gz) Translating

Info Catalog (coreutils.info.gz) Character sets (coreutils.info.gz) tr invocation (coreutils.info.gz) Squeezing
 
 9.1.2 Translating
 -----------------
 
 'tr' performs translation when SET1 and SET2 are both given and the
 '--delete' ('-d') option is not given.  'tr' translates each character
 of its input that is in SET1 to the corresponding character in SET2.
 Characters not in SET1 are passed through unchanged.  When a character
 appears more than once in SET1 and the corresponding characters in SET2
 are not all the same, only the final one is used.  For example, these
 two commands are equivalent:
 
      tr aaa xyz
      tr a z
 
    A common use of 'tr' is to convert lowercase characters to uppercase.
 This can be done in many ways.  Here are three of them:
 
      tr abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ
      tr a-z A-Z
      tr '[:lower:]' '[:upper:]'
 
 But note that using ranges like 'a-z' above is not portable.
 
    When 'tr' is performing translation, SET1 and SET2 typically have the
 same length.  If SET1 is shorter than SET2, the extra characters at the
 end of SET2 are ignored.
 
    On the other hand, making SET1 longer than SET2 is not portable;
 POSIX says that the result is undefined.  In this situation, BSD 'tr'
 pads SET2 to the length of SET1 by repeating the last character of SET2
 as many times as necessary.  System V 'tr' truncates SET1 to the length
 of SET2.
 
    By default, GNU 'tr' handles this case like BSD 'tr'.  When the
 '--truncate-set1' ('-t') option is given, GNU 'tr' handles this case
 like the System V 'tr' instead.  This option is ignored for operations
 other than translation.
 
    Acting like System V 'tr' in this case breaks the relatively common
 BSD idiom:
 
      tr -cs A-Za-z0-9 '\012'
 
 because it converts only zero bytes (the first element in the complement
 of SET1), rather than all non-alphanumerics, to newlines.
 
 By the way, the above idiom is not portable because it uses ranges, and
 it assumes that the octal code for newline is 012.  Assuming a POSIX
 compliant 'tr', here is a better way to write it:
 
      tr -cs '[:alnum:]' '[\n*]'
 
Info Catalog (coreutils.info.gz) Character sets (coreutils.info.gz) tr invocation (coreutils.info.gz) Squeezing
automatically generated by info2html