By using TR, you can easily implement many of the most basic features of SED. You can see TR as a (extremely) simplified variant of sed: it can replace another character with one character, or it can completely remove some characters. You can also use it to remove duplicate characters. This is what all TR can do.
Command format
TR [option] ["string1"] ["string2"] < file
The common options are:
The default option. When there is no option, TR defaults to the substitution operation, which is to replace the characters that appear in the file with the characters in the string2, and note that the substitution relationship is string1.
-c option to replace string1 with the complement of the characters in string1, where the character set is ASCII.
-d option to delete all characters in the file that appear in string1.
-S option to delete the characters that are duplicated in the file and appear in the string1, leaving only one.
The-c option, when used, simply replaces the string1 with the current complement, as in the
[Email protected] ~]#Echo "Hello world,root,2012"|TR-C"0-9" "*"***************** -*[[email protected] ~]#Echo "Hello world,root,2012"|TR "0-9" "*"Hello World,root,****[[Email protected]~]#
As can be seen, we use 0-9, add-C option, will replace 0-9 with its complement, when the complement naturally does not contain 0-9, and contains a lot of other characters, then all the other words nonalphanumeric replaced by the * number, but does not contain numbers.
Range of values for strings
When you specify the contents of a string or string2, you can only use single-character or string ranges or lists.
A string that consists of characters in A-Z (A-Z).
[A-z] A string consisting of characters within a-Z.
[0-9] number string.
\octal a three-bit octal number that corresponds to a valid ASCII character.
[O*n] indicates that the character O repeats the specified number of times N. therefore [o*2] matches the OO string.
Control how characters are expressed differently
Shorthand notation meaning octal method
\a ctrl-g Ringtones \007
\b Ctrl-h backspace \010
\f Ctrl-l Walk the line to change page \014
\ ctrl-j New Line \012
\ r Ctrl-m Enter \015
\ t ctrl-i Tab key \011
\v Ctrl-x \030 Note These control characters, if you want to enter under Linux, as we may need to enter ^m this character, just ctrl+v+m simultaneously press.
Character substitution
[Email protected] ~]#Echo "Hello World"|TR "A- z" "A- z"HELLO World[[email protected]~]#Echo "Hello World"|TR "a-l" "A- z"HELLo World[[email protected]~]#Echo "Hello World"|TR "A- z" "a-h"hehhh HHHHD
The first line of output is to change lowercase to uppercase.
The second line of output replaces the a-l in lowercase with a-l, and the characters from the lowercase L are not replaced.
The third line of output converts the a-h in lowercase to a-h, and the H character to H, since the substitution space for the latter is not larger than the preceding character space, so repeat the following H, which is the equivalent of the following character a-hhh ... Hhhhh.
If we want to make a case conversion, you can enter it as follows:
TR "A-Z" "A-Z" < Inputfile
Remove repeating characters
At this time, the option used is the-s option, such as:
[Email protected] ~]#Echo "Hello World,root"|TR-S"ao"Hello World,rot[[email protected]~]#Echo "Hello World,root"|TR-S"Lo"helo world,rot[[email protected]~]#Echo "Hello World,root"|TR-S"A- z"helo world,rot[[email protected]~]#Echo "Hello World,root"|TR-S"0-9"Hello World,root
The first line means that the repeating characters contained in the "AO" character set in the input string are stripped, leaving only one. Because "Hello World,root", only o satisfies the condition, so the root becomes rot, the middle of the two o into one.
The second row of Hello and root two characters are compressed.
The third line removes all but the complex characters from the A-Z.
The third line means that the characters in the string that repeat and repeat characters are in the 0-9 character set are removed, not here.
If we want to get rid of empty lines, we can do this:
Tr-s "\ n" < Inputfile or tr-s "\012" <inputfile//The two are the same.
is to remove the repeated line breaks, leaving only one.
Delete character
The-D option is similar to the-s option except that the-D option removes all occurrences of the character.
[Email protected] ~]#Echo "Hello World,root"|TR-D"a-h"llo Worl,root[[email protected]~]#Echo "Hello world,root,2012"|TR-D"A- z" ,, -[[Email protected]~]#Echo "Hello world,root,2012"|TR-D"0-9"Hello World,root,
Description of the TR editor commands under Linux