Translate, squeeze, and/or delete characters from standard input, writing to standard output
tr [OPTION]... SET1 [SET2]
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.
SET1 and SET2 are specified as strings of characters. Most represent themselves. Interpreted sequences are:
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.
The following command converts all lowercase letters in the input string to uppercase.
$ echo "hello world" | tr '[:lower:]' '[:upper:]' HELLO WORLD
The following command deletes all occurrences of the characters ',' and '.' from the input string.
$ echo "Hello, world." | tr -d ',.' Hello world
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
The following command squeezes multiple consecutive spaces into a single space in the input string
$ echo "Hello world" | tr -s ' ' Hello world