The TRTR command can replace, compress, and delete characters from standard input
Grammar
TR Options (SET1 SET2)
Options
- C or--complerment: Supersedes all characters that are not part of the first character set;-D or--delete: Deletes all characters belonging to the first character set;-S or--squeeze- Repeats: the consecutive repeating character is represented by a single character;-T or--truncate-set1: First remove the characters from the first character set that are more than the second character set.
Parameters
SET1: Specifies the original character set to be converted or deleted.
When performing a conversion operation, the target character set of the transformation must be specified using the parameter "SET2".
However, the parameter "SET2" is not required when performing the delete operation;
SET2: Specifies the target character set to convert to.
Instance
Converts an input character from uppercase to lowercase
Last|TR 'A- z' 'A- z' 'A- z'And'A- z'are all collections such as:'abd-}'、'BB.,'、'a-de-h'、'a-c0-9'All belong to the collection, can be used in the collection'\ n'、'\ t', you can use other ASCII characters.
Using TR to delete characters
cat/etc/passwd | Tr-d ': '
To convert a tab to a space:
Cat TR ' \ t ' ' '
A character set complement that deletes all characters from the input text that are not in the complement:
Echo 1 2 c*/cc34tr'0-9 \ n' results:12 34 In this example, the complement contains a number 0~9, a space, and a newline character \ n, so it has not been deleted and all other characters have been deleted.
To compress characters with TR, you can compress the repeated characters in the input:
Echo " Thissss is a text linnnnnnne. " TR ' SN ' This was a text line.
Clever use of TR to do number addition operation:
Echo 1 2 3 4 5 6 7 8 9 Xargs Echo $[$ (tr'\ n'+' ) 0 ]
Remove the ' ^m ' character from the Windows file ' cause ':
Cat file TR " \ r " " \ n "
Cat file TR " \ r "
The character classes you can use:
[: Alnum:]: Letters and Numbers [: Alpha:]: letter [: Cntrl:]: Control (nonprinting) character [:d Igit:]: Number [: Graph:]: graphic character [: Lower:]: lowercase letter [:p rint:]: printable character [:p UNCT:] : Punctuation [: Space:]: blank character [: Upper:]: Uppercase [: xdigit:]: hexadecimal character
How to use
TR ' [: Lower:] ' ' [: Upper:] '
LINUX:TR command Explanation