Tr
Usage: tr [option]... set1 [set2] replaces, reduces, and/or removes characters from standard input and writes the results to standard output. -c, -C, --complement first complement set1 -d, --delete Delete the contents of the matching set1 , does not replace -s, --squeeze-repeats if the character matching the set1 has a continuous in the input sequence repetition, which is uniformly shrunk to one character at the time of substitution -t, --truncate-set1 set1 The length of the first and set2 equal --help Display this help message and exit --version display version information and exit set is a set of strings, which can generally be understood by literal means. The parsing sequence is as follows: \nnn octal value is nnn (1 to 3 digits) \\ anti-Slash \a Terminal ringing \b backspace \f page Break \n line break \r enter \t Horizontal Tab \v Vertical Tab character 1-character 2 all characters [characters from character 1 to character 2 in ascending increments of characters *] in set2 , the specified characters are copied continuously until they match the length of the set 1 [characters * Number] Performs a specified number of copies of a character, if the number of times starts with 0 is treated as an octal number [:alnum:] all letters and numbers [: alpha:] all letters [:blank:] all horizontal white-space characters [: cntrl:] all the control characters [:d igit:] all the numbers [:graph:] InstituteSome printable characters, not including spaces [:lower:] all lowercase letters [:p rint:] All printable characters, including spaces [:p unct:] all punctuation characters [:space:] All white space characters arranged horizontally or vertically [:upper:] all uppercase letters [:xdigit:] All hexadecimal number [= characters =] all characters equal to the specified characters are given only in set1 and set2 , without-d option is not replaced. The-t option is only possible with replacements. If required set2 will be added to the same length as set1 by adding the original last character at the end. The extra characters in the set2 will be omitted. Only [:lower:] and [: Upper:] Expand characters in ascending order, and case conversions are represented in pairs in set2 when used for substitution. -s acts on SET1, neither replace nor delete, otherwise use set2 reduction after substitution or expansion.
Specific examples:Simple substitution character
Echo ABC > ACAT a Abccat a |tr "a" "ABC" Abc[[email protected] mnt]# cat a |tr "a" "ABC" Abc[[email protected] mnt]# CA T a |tr "a" "CJW" Cbc[[email protected] mnt]# cat a |tr "a" "CJW" Cbc[[email protected] mnt]# cat a |tr "a" "s" SBC
You can see that there is a bit limit to modify only the source of the same length string
Replace case
[email protected] mnt]# cat a |tr [a-z] [A-Z][BC This is not possible [[email protected] mnt]# Cat a |tr A-Z A-to-a-kind ABC so OK, or in [] plus ' ' can also
Replace letters with numbers
[[email protected] mnt]# Cat a |tr a-c 2-5234[[email protected] mnt]# cat a |tr A-b 2-523c
It's also a correspondence relationship.
Delete character
[email protected] mnt]# cat a|tr-d ABC
Delete a tab and enter
[[email protected] mnt]# cat ABC |tr-d "\n\t" Abctabtab[[email protected] mnt]# cat ABC |tr-d "\n\t"
delete consecutive repeating letters
[[email protected] mnt]# cat ABCD |tr-s [a-za-z] PS: This is not valid OH: ~ Output is the original string accbbbbdd [[email protected] mnt]# cat ABCD |tr-s ' [a-za-z] ' Acbdok can be [[email protected] mnt]# cat ABCD |tr-s A-ZA-ZACBD so also OK
Delete empty lines
[Email protected] mnt]# echo > Abcd[[email protected] mnt]# cat abcd |tr-d "\ n" [[email protected] mnt]# vim ABCD
There's no space.
tab symbol \011 space, \040
[[email protected] mnt]# cat abcdtab tab TAB PS: Empty between tab [[email Prote CTED] mnt]# cat abcd|tr-s ' \011 ' \040 ' tab tab tab
Linux command-tr