tr mainly used for data read from the standard input the result set mapping, compression and character delete character. It first reads the standard input will be sorted in some way and then wrap, and then re-do the relevant processing parameters according to a given command.
TR [Options] [SET1] [SET2] - C: using the complement of SET1 - D: Delete character - S: Compression characters -t: Truncated SET1, so that the length of SET1 and SET2 same
tr map
If both SET1 and SET2, SET1 sucked by location symbols correspond to a symbol mapping in SET2. In other words, a corresponding replace.
Tr stdin after receiving the first result will be marked according to some new line symbol. E.g:
[root@localhost ~]# ls 4 a ab.sh anaconda-ks.cfg b c case1.sh d one key.txt rmbackup select1.sh select2.s
Replaces spaces tabs. Since the received data is a tr sorted wrap, so the results only been replaced by "one key.txt" spaces.
[root@localhost ~]# ls | tr " " "\t" 4 a ab.sh anaconda-ks.cfg b c case1.sh d one key.txt rmbackup select1.sh select2.s
The reason that is tr mapping rather than replace, because the two result sets when replacing the symbol position is one to one. If SET1 is shorter than SET2, the extra parts SET2 are ignored, if SET1 longer than SET2, POSIX think it's unreasonable, but also can perform, but some unexpected results, see below. For example the following example, because only one symbol SET1 "\ n", then the Y SET2 replaced is ignored.
[root@localhost ~]# tr "\n" "XY" axbxcxdifxidfjsiykdsjfjyyyfdklsjy axbxcxdifxidfjsiykdsjfjyyyfdklsjyX
This enables simple encryption and decryption.
[root@localhost ~]# echo "12345" | tr "0-9" "9876543210" 87654 [root@localhost ~]# echo "87654" | tr "0-9" "9876543210" 12345
The above process is to the left of the pipe corresponds to 0-9 12345 0123456789 expandable and mapped onto corresponding bit digital SET2. Decryption is the same reason.
There is a ROT13 encryption algorithm to encrypt and decrypt it using a set of characters. It's a bit letters and letter-bit SET1 SET2 completely reversed in pairs. For example SET1 specified symbol is "axy", if SET2 want to map it as "opq", shall be extended to SET1 "axyopq", SET2 extended "opqaxy", ultimately (a, x, y, o, p, q) and (o, p, q, a, x, y), so that (a, o) and (o, a) can be successful pairing. And then expand it to AZ and az, so-called ROT13 encryption, can even be added by 0-9 and 9-0 correspond. Here SET1 and SET2 is a corresponding formula.
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm
Now Encryption "I love you"
[root@localhost ~]# echo "I love you" | tr "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" "NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm" V ybir lbh
The "V ybir lbh" decryption.
[root@localhost ~]# echo "V ybir lbh" | tr "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" "NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm" I love you
Completely replace the corresponding
By default, when the specified ratio SET1 SET2 characters long, starting from the position corresponding to the last, the remaining characters are SET1 and SET2 corresponding to the last character. If SET1 = [1234], SET2 = [abc], corresponding to the 3 c, 4 also corresponds to c, if at this time the operation target appearing in tr 3 or 4 are replaced with c.
Use -t may truncate long character than SET1 SET2, such as the above excess cut 4, SET1 = [123] and SET2 = [abc] to achieve a complete correspondence.
[root @ localhost ~] # CAT x.txt NO Mark subjectid the Name Notes . 1 longshuai 001 56 is failing 2 gaoxiaofang 001 60 pass . 3 zhangsan 001 50 fail . 4 Lisi 001 80 pass . 5 wangwu 001 90 pass [the root @ localhost ~] # CAT x.txt | TR " Fang " " Jin " # results n and g are replaced to n NO Nime subjectid known as the Mirk Notes . 1 lonnshuii 001 56 Fail 2 nioxiiojinn 001 60 pass . 3 zhinnsin 001 50 fail . 4 Lisi 001 80 pass . 5 winnwu 001 90 pass [the root @ localhost ~] # CAT x.txt | TR -t " Fang " " Jin " # G is truncated, corresponds to replace only the fan Jin nO Nime subjectid known as the Mirk Notes . 1 longshuii 001 56 is failing 2 gioxiiojing 001 60 passing 3 zhingsin 001 50 fail . 4 Lisi 001 80 pass . 5 wingwu 001 90 pass
Compression symbol
This function too cool.
tr -s [SET1] [SET2]
If you do not specify SET2, then only been compressed, not replaced. SET1 plurality of characters can be specified, so that each character will be compressed, for example tr -s "0a"
, that will compress consecutive 0 will compress consecutive a. If SET2 is specified, one to one further compressed replaced.
If the contents of the file are as follows x.txt, spaces in some places, less in some places, that this is not a file format.
[the root @ localhost ~] # CAT x.txt NO Mark subjectid the Name Notes . 1 longshuai 001 56 is failing 2 gaoxiaofang 001 60 pass . 3 zhangsan 001 50 fail . 4 Lisi 001 80 pass . 5 wangwu 001 90 pass
Use tr compression space so that it becomes the rule.
[the root @ localhost ~] # CAT x.txt | TR -s " " NO Mark subjectid the Name Notes . 1 longshuai 001 56 is failing 2 gaoxiaofang 001 60 pass . 3 zhangsan 001 50 fail . 4 Lisi 001 80 pass . 5 wangwu 001 90 pass
If you specify SET2, if replaced by "-."
[the root @ localhost ~] # CAT x.txt | TR -s " " " - " NO -Name-subjectid-Mark- Notes . 1 -longshuai- 001 - 56 is - failed 2 -gaoxiaofang- 001 - 60 - pass . 3 - zhangsan- 001 - 50 - failed . 4 -lisi- 001 - 80 - pass . 5 -wangwu- 001 - 90 - pass
Delete symbols and complement
tr -d
It is to remove the specified symbol, can only take a SET1.
[root @ localhost ~] # CAT x.txt | TR -d " " NONameSubjectIDMark Remarks 1longshuai00156 failed 2gaoxiaofang00160 pass 3zhangsan00150 fail 4lisi00180 pass 5wangwu00190 fail
tr -c SET1 SET2
It is a standard input by a character set SET1 complement, and complement part replace all of SET2, soon not present standard input SET1 but not present in SET2 characters replaced character. However, if the specified character SET2 is greater than 1, taking only the last character of the replacement characters. When should use the -c -c SET1 as a whole, not separate them.
E.g:
[root@localhost ~]# echo "abcdefo"| tr -c "ao" "y" ayyyyyoy
Standard input "abcdefo" according SET1 = "ao" complement is determined bcdef, replace them with y, is the result ayyyyyo, but the final result of a multi-y plane and subsequently the command prompt. This is because part of \ n ao is the complement of the tail of abcdefo, and replace it with a y. If you do not want to replace the last \ n, you can specify \ n in SET1 in.
[root@localhost ~]# echo "abcdefo"| tr -c "ao\n" "y" ayyyyyo
If SET2 designated plurality of characters, a character will be taken only as a last alternative character.
[root@localhost ~]# echo "abcdefo"| tr -c "ao\n" "ay" ayyyyyo
[root@localhost ~]# echo "abcdefo"| tr -c "ao\n" "yb" abbbbbo
used with "-c" regular and "-d", as tr -d -c SET1
. It is the first implementation of "-c SET1" to obtain the complement of SET1, and then delete this complement. In other words, the end result is exactly match the characters in SET1. Note, "- d" must be placed in front of the "-c", otherwise it is resolved to tr -c SET1 SET2
, it is not deleted complement executed, but instead complement for the "-d" the last character d.
[root @ localhost ~] # echo " One 2 Three TWO. 3. 1 " | TR -d -C " [0-9] \ n- " 123 # and digital breaks complementing set, and delete the complementary set of symbols [root @localhost ~] # echo " One TWO 1 2 3 Three " | TR -d -c " [0-9] \ the n- " 1 2 3 # another space complementing set [root @ localhost ~] # echo " One Three TWO 2. 3. 1 " | TR -C " [0-9] \ n- " - D dddd1ddddd2ddddddd3 # -d option in - back option is to replace the behavior of c [the root @ localhost ~] # echo " One 2 Three TWO. 3. 1 " | TR -d -C " [A-zA-Z] \ n- " oneTwoThree # Preserve [the root @ localhost ~] # echo " One 2 Three TWO. 3. 1 " | TR -d -C " [a-zA-Z] \ n- " One TWO Three reserved reserved # alphabet as well as the space
We can see the complement of the above, in fact, specified in [0-9] and [az] is a character class, the final results show that this class of objects.
You can use the following characters in tr in class. These classes can also be used in some other commands.
[:alnum:]
All numbers and letters. [:alpha:]
All letters. [:blank:]
All levels of blank space = + tab. [:cntrl:]
All control characters (non-printable characters), octal 0-37 and 177 corresponding character in ascii del table. [:digit:]
All figures. [:graph:]
All printable characters, no spaces = Digital + letters + punctuation. [:lower:]
All lowercase letters. [:print:]
All printable characters, spaces = number + space + letters + punctuation. [:punct:]
All punctuation. [:space:]
All horizontal or vertical blank space = breaks tab + + + + vertical page break tab + Enter key. [:upper:]
All capital letters. [:xdigit:]
All hexadecimal numbers.
For example, using the following method. For example [: upper:] is equivalent to [AZ], [: digit:] is equivalent to [0-9].
[root@localhost ~]# echo "one ONE 1 two TWO 2 three THREE 3" | tr -d -c "[:upper:] \n" ONE TWO THREE [root@localhost ~]# echo "one ONE 1 two TWO 2 three THREE 3" | tr -d -c "[:alpha:] \n" one ONE two TWO three THREE [root@localhost ~]# echo "one ONE 1 two TWO 2 three THREE 3" | tr -d -c "[:digit:] \n" 1 2 3