Skip to main content

tr

The tr (translate) command is used to translate, squeeze, or delete characters from standard input, writing to standard output. It is frequently used for changing case, replacing delimiters, or removing specific characters.

Basic Syntax

tr [OPTION]... SET1 [SET2]

Common Options

OptionDescription
-d, --deleteDelete characters in SET1, do not translate.
-s, --squeeze-repeatsReplace each sequence of a repeated character that is listed in the last specified SET, with a single occurrence of that character.
-c, -C, --complementUse the complement of SET1.

Real-world Examples

  1. Convert lowercase to uppercase:

    cat text.txt | tr 'a-z' 'A-Z'
  2. Replace spaces with underscores:

    echo "hello world" | tr ' ' '_'
  3. Delete all numeric characters:

    echo "password123" | tr -d '0-9'
  4. Squeeze multiple spaces into a single space:

    echo "this    has    too   many   spaces" | tr -s ' '