1. Description
Tr-translate or delete characters
Tr [Option]... set1 [set2]
Translate, squeeze, and/or delete characters from standard input, writing to standard output. Sets are specified as strings of characters.
TR is used to convert characters from standard input through replacement or deletion. Use TrProvide two strings: Set1Used for query, Set2Used to process the conversion.TrExecution time,String1The characters in are mapped to strings.2Characters in, Then perform the conversion operation.
2,Option
-C requires the character set to ASCII . Use set2 replace set1 use set2 the character replacement is not in set1 character
-D DeleteSet1All input characters.
-S deletes all recurring character sequences and only keeps the first
Translation occurs if-D is not given and both set1 and set2 appear.
3. character range
[A-Z]: [A-Z]: [0-9]: digit string
\ Octal: octal number of three digits,Corresponding validASCIICharacter
[Char * repeat] Repeat copies of char, repeat octal if starting with 0
4. Example
1) Remove duplicate characters
Tr-s "[A-Z]" <example.txt
Cat example.txt | tr-s "[A-Z]"
2) Delete empty rows
Cat example.txt | tr-s "[\ n]"
3) Small write in upper case
Cat example.txt | TR "[A-Z]" "[A-Z]"
Cat example.txt | TR "[: lower:]" [: Upper:]"
4) replace
CAT/etc/passwd | tr-s "[:]" [\ t]"
5)Tr-s "[\ n]" <Test> test11 //Input file isTest,The output file isTest1
6)Cat test.txt | tr-d "[0-9] [:]" //Delete numeric characters and colons
7) CAT test.txt | tr-CS" [A-Z] [A-Z] "\ n" //-C : replace all characters except letters with line breaks; -S : delete redundant linefeeds
5. PassTrAll functions can be implemented throughSed. Converts uppercase and lowercase letters, and removes unnecessary characters.
Reference
[ 1 ] http://blog.chinaunix.net/space.php? Uid = 1813014 & Do = Blog & cuid = 364891
[ 2 ] http://unix-cd.com/vc/www/39/2007-11/9971.html
【3]Http://bbs.chinaunix.net/thread-2106256-1-1.html
Http://blog.chinaunix.net/space.php? Uid = 1813014 & Do = Blog & cuid = 364891
[ 4 ] http://www.2cto.com/ OS /201109/104590.html