The TR command is used to replace the contents of the standard input. For example
# echo ' HELLO World!!! ' | TR "A-Z" "A-Z"
Hello World!!!
Here the "A-Z", "A-Z" all represent the collection, the shell script defines the collection type is very simple, that is, the specified collection sequence, but for the upper case, must not enter all the collection type, can be defined by the "Start character-end character" method of the collection. Using the TR command in conjunction with a collection can solve many complex problems.
Use TR for data encryption, decryption:
# echo 456 | TR "0-9" "9876543210"
543
# echo 543 | TR "9876543210" "0-9"
55W
TR for character substitution:
# Cat Dept
Ten ACCOUNTING NEW YORK
DALLAS
SALES CHICAGO
OPERATIONS BOSTON
# Cat Dept | TR "\ T" "" "
Ten ACCOUNTING NEW YORK
DALLAS
SALES CHICAGO
OPERATIONS BOSTON
Specify the-d parameter to delete the specified string:
# echo ' Hello 123 world 456!!! ' | Tr-d ' {0-9} '
Hello World!!!
Specify the-c parameter for complement processing, usually with-D, to delete characters that do not meet the criteria:
# echo ' Hello 123 world 456!!! ' | Tr-d-c ' {0-9 \ n} '
123 456
The-s parameter in the TR command compresses characters, compressing successive repeating characters into characters, and the most common scenario is to compress the blank lattice:
# echo ' Hello 123 world 456!!! ' | Tr-s "
Hello 123 World 456!!!
Tip: Use TR to add the list of characters in the file:
# seq 5 | Echo $[$ (tr ' \ n ' + ') 0]
15
Explanation: After replacing \ n with +, the script output becomes $[1+2+3+4+5+0], which can be calculated directly, eliminating the hassle of looping the numbers.
TR can also use a variety of different character classes like a collection.
The shell script's TR command is used