Category

Linux Command


Usage

tr [OPTION]... SET1 [SET2]


Manual

Required arguments

  • SET1 [SET2]: The sets of characters used for translation or deletion. It represents the input character set that you want to process and modify. The tr command performs character translation or deletion based on the relationship between the characters in SET1 and SET2. Here's a breakdown of the concept:

    SET1: This represents the set of characters that tr will search for in the input text to determine whether to translate or delete them. It defines the characters you want to replace, delete, or modify.

    SET2: This represents the set of characters that tr will use for the replacement or deletion. It defines the characters that will replace the corresponding characters in SET1 or be deleted altogether.

    The relationship between SET1 and SET2 determines how the tr command operates:

    If SET2 is shorter than SET1, the extra characters in SET1 will be deleted from the input.

    If SET2 is longer than SET1, the extra characters in SET2 will be ignored.

    If SET2 is the same length as SET1, the characters will be replaced one-to-one. For example, in the command

    tr 'abc' 'xyz'
    SET1 is 'abc', and SET2 is 'xyz'. It means that 'a' will be replaced with 'x', 'b' with 'y', and 'c' with 'z' in the input text.

Options

  • -c, -C, --complement: use the complement of SET1
  • -d, --delete: delete characters in SET1, do not translate
  • -s, --squeeze-repeats: replace each input sequence of a repeated character that is listed in SET1 with a single occurrence of that character
  • -t, --truncate-set1: first truncate SET1 to length of SET2
  • --help: display this help and exit
  • --version: output version information and exit


SET1 and SET2 are specified as strings of characters. Most represent themselves. Interpreted sequences are:

  • \NNN: character with octal value NNN (1 to 3 octal digits)
  • \\\: backslash
  • \a: audible BEL
  • \b: backspace
  • \f: form feed
  • \n: new line
  • \r: return
  • \t: horizontal tab
  • \v: vertical tab
  • CHAR1-CHAR2: all characters from CHAR1 to CHAR2 in ascending order
  • [CHAR*]: in SET2, copies of CHAR until length of SET1
  • [CHAR*REPEAT]REPEAT copies of CHAR, REPEAT octal if starting with 0
  • [:alnum:]: all letters and digits
  • [:alpha:]: all letters
  • [:blank:]: all horizontal whitespace
  • [:cntrl:]: all control characters
  • [:digit:]: all digits
  • [:graph:]: all printable characters, not including space
  • [:lower:]: all lower case letters
  • [:print:]: all printable characters, including space
  • [:punct:]: all punctuation characters
  • [:space:]: all horizontal or vertical whitespace
  • [:upper:]: all upper case letters
  • [:xdigit:]: all hexadecimal digits
  • [=CHAR=]: all characters which are equivalent to CHAR


Translation occurs if -d is not given and both SET1 and SET2 appear. -t may be used only when translating. SET2 is extended to length of SET1 by repeating its last character as necessary. Excess characters of SET2 are ignored. Only [:lower:] and [:upper:] are guaranteed to expand in ascending order;  sed in SET2 while translating, they may only be used in pairs to specify case conversion. -s uses SET1 if not translating nor deleting; else squeezing uses SET2 and occurs after translation or deletion.

Examples

Convert lowercase to uppercase

The following command converts all lowercase letters in the input string to uppercase.

$ echo "hello world" | tr '[:lower:]' '[:upper:]'

HELLO WORLD
Delete specific characters

The following command deletes all occurrences of the characters ',' and '.' from the input string.

$ echo "Hello, world." | tr -d ',.'

Hello world
Replace characters

The following command converts a csv (',') file to a tsv ('\t') file:

$ cat regions.csv| tr , '\t' | head
chr1    10000    12114    _    0
chr1    13000    15114    _    0
chr1    14000    16114    _    0
chr1    15000    17114    _    0
Squeeze repeated characters

The following command squeezes multiple consecutive spaces into a single space in the input string

$ echo "Hello   world" | tr -s ' '

Hello world

 


Share your experience or ask a question