Introduction
The TR command can replace, compress, and delete characters from standard input. It can turn a set of characters into another set of characters.
Grammar
TR [OPTION] ... SET1 [SET2]
Note: SET2 is optional
OPTION:
without parameters: replace each character in the SET2 with each character in the SET1, the character is the order substitution, and if the character length of the SET1 is greater than SET2, the characters that are extra in SET1 are replaced with the last character in SET2. -T: replace each character in the SET2 with each character in SET1, character order 1 to 1, regardless of SET1 or SET2 which is long, replace only the corresponding character, the extra-out is not replaced. -C: Replaces characters specified in SET1 with characters specified in SET2, if SET2 contains more than one character and is replaced with only the last character. -D: removes the specified character from SET1, and there is no SET2-S: replaces the contiguous consecutive repeating character specified in SET1 with a single character.
Character Set Code:
[: Alnum:]: Letters and numbers can be used instead of ' a-za-z0-9 ' [: Alpha:]: Letters can be used instead of ' a-za-z ' [: Cntrl:]: Controlling (nonprinting) characters [: Digit:]: number, can be used instead of ' 0-9 ' [: Graph:]: Graphic character [: Lower:]: lowercase letter, can be used instead of ' A-Z ' [:print:] : printable character [:p UNCT:]: punctuation [: Space:]: blank character [: Upper:]: Uppercase, can be used instead of ' A-Z ' [: Xdigit:] : Hexadecimal characters
\ \ backslash \A terminal beep \ b backspace \ f page change \ n Line Break \r enter \t horizontal tab \v Vertical tab
A null character
Example:
1. Replace SET2 with SET1 with no parameters and SET1 longer than SET2
[Root@localhost ~] # echo "AAAA1BBBB2CCCC3" | tr ' abc '11AA122BB222CC3
A is replaced with 1,b replaced by 2,c replaced by 2
2. Replace the SET2 with SET1 with no parameters, and the SET1 length is less than SET2
[Root@localhost ~] # echo "AAAA1BBBB2CCCC3" | tr ' AB ' 123' 11aa122bb2cccc3
A is replaced with 1,b replaced by 2
3.-t parameters
[Root@localhost ~] # echo "AAAA1BBBB2CCCC3" | tr-t ' abc ' 11aa122bb2cccc3[root@localhost ~] # echo "AAAA1BBBB2CCCC3" | tr-t ' AB ' 123 '11aa122bb2cccc3
are replaced by a 1,b and replaced by 2.
4. Delete the specified character,-D
[Root@localhost ~] # aa1bb2cc3[root@localhost ~]# echo "AAAA1BBBB2CCCC3" | tr-d-C ' a-z\n ' AABBCC
The first is to remove lowercase characters, and the second is to remove any character other than lowercase, which is the same as using the character set.
[Root@localhost ~] # aa1bb2cc3[root@localhost ~]# echo "AAAA1BBBB2CCCC3" | tr-d-C ' [: lower:]\n 'aabbcc
5. Replace consecutive characters,-s
[Root@localhost ~] # echo "AAAA1BBBB2CCCC3" | tr-s ' A-za-z ' aa1bb2cc3[root@localhost ~] # echo "AAAA1BBBB2CCCC3" | tr-s ' [: alnum:]\n 'aa1bb2cc3
Both of the above methods replace repeated characters with a single character
Other usage: replace null characters with line breaks
[Root@localhost~]#cat/proc/4518/environ \ nterm=xtermpath=/sbin:/usr/sbin:/bin:/usr/bin:/usr/Local/mysql/binpwd=/usr/Local/mysqlshlvl=2oldpwd=/_=/usr/Local/mysql/bin/mysqld_safecat:n:Nosuch file or directory
[Root@localhost~]#cat/proc/4518/environ |tr ' \ n 'term=Xtermpath=/sbin:/usr/sbin:/bin:/usr/bin:/usr/Local/mysql/binpwd=/usr/Local/MYSQLSHLVL=2oldpwd=/_=/usr/Local/mysql/bin/Mysqld_safe[root@localhost~]#
Split a sentence into multiple lines.
Summary
There is a misunderstanding that can easily be misinterpreted as Set1,set2 is a combination of characters, in fact, is not the case; SET1 and SET2 are all values of a single character substitution, such as ' AB ' do not interpret AB as a combination.
Note: pursuer.chen Blog:http://www.cnblogs.com/chenmh This site all the essays are original, welcome to reprint, but reprint must indicate the source of the article, and at the beginning of the article clearly give the link. Welcome to the exchange of discussions |
Linux TR Command