tr

Replace, compress and delete characters

Supplementary instructions

tr command can replace, compress and delete characters from standard input. It can change one set of characters into another set of characters, and is often used to write beautiful one-line commands, which is very powerful.

grammar

tr (option) (parameter)

Options

-c or --complerment: replace all characters that do not belong to the first character set;
-d or --delete: delete all characters belonging to the first character set;
-s or --squeeze-repeats: Represent consecutively repeated characters as a single character;
-t or --truncate-set1: First delete the characters in the first character set that are more than the second character set.

Parameters

*Character set 1: Specify the original character set to be converted or deleted. When performing a conversion operation, the parameter "Character Set 2" must be used to specify the target character set for conversion. But when performing a delete operation, the parameter "Character Set 2" is not required; *Character set 2: Specify the target character set to be converted to.

Example

Convert input characters from uppercase to lowercase:

echo "HELLO WORLD" | tr 'A-Z' 'a-z'
hello world

'A-Z' and 'a-z' are both sets, and the sets can be customized, for example: 'ABD-}', 'bB.,', 'a-de-h', 'a-c0-9' Belongs to a collection. '\n', '\t' can be used in the collection, and other ASCII characters can be used.

Use tr to remove characters:

echo "hello 123 world 456" | tr -d '0-9'
hello world

Convert tabs to spaces:

cat text | tr '\t' ' '

Character set complement, remove all characters from the input text that are not in the complement:

echo aa.,a 1 b#$bb 2 c*/cc 3 ddd 4 | tr -d -c '0-9 \n'
  1 2 3 4

In this example, the complement contains the numbers 0~9, spaces and newline characters \n, so they are not deleted, and all other characters are deleted.

Using tr to compress characters can compress repeated characters in the input:

echo "thissss is a text linnnnnnne." | tr -s ' sn'
this is a text line.

Cleverly use tr to add numbers:

echo 1 2 3 4 5 6 7 8 9 | xargs -n1 | echo $[ $(tr '\n' '+') 0 ]

Remove '^M' characters "caused" by Windows files:

cat file | tr -s "\r" "\n" > new_file
or
cat file | tr -d "\r" > new_file

Character classes that can be used by tr:

[:alnum:]: letters and numbers
[:alpha:]: letters
[:cntrl:]: control (non-printing) characters
[:digit:]digit
[:graph:]: graphic characters
[:lower:]: lowercase letters
[:print:]: Printable characters
[:punct:]: punctuation mark
[:space:]: white space character
[:upper:]: uppercase letters
[:xdigit:]: Hexadecimal characters

How to use:

tr '[:lower:]' '[:upper:]'