Usage: TR [Option]... set1 [set2] replaces, reduces, and/or deletes characters from the standard input, and writes the result to the standard output. -C,-C, -- complement first supplements set1-D, -- delete deletes the content that matches set1, and does not replace-s, -- Squeeze-repeats if the characters matching set1 have continuous duplicates in the input sequence, they will be reduced to-T in the replacement process, -- The truncate-set1 first intercepts the length of set1 to be equal to set2 -- help displays this help information and exits -- version displays version information and exits
We can see that this command is mainly used for character filtering. The options are optional because they are placed in square brackets. Do not use any options first:
$:~/csat/packages$ tr 'eaiou' 'fbjpv'i have a dreamj hbvf b drfbm
From the results, we can see that TR is used for character conversion (in character conversion units rather than character sequences ), replace all occurrences of the characters defined in the character set with those corresponding to the position in the character set. It looks like it can be used as a small encryption method. Try the case where the position length does not match:
$:~/csat/packages$ tr 'eaiou' 'fbjpvt'i have a dream , and you ?j hbvf b drfbm , bnd ypv ?
B. The length of set1 is greater than set2: the appearance of multiple characters corresponding to the position of set1 is replaced with the appearance of the last character of set2.
$:~/csat/packages$ tr 'eaiou' 'fbjp'i love you, u know ?j lpvf ypp, p knpw ?
O and u are replaced with P. The-T option seems to be somewhat correlated. Try:
$:~/csat/packages$ tr -t 'eaiou' 'fbj'i love youj lovf you
Now O and u are no longer replaced. You can understand the role of-t. It is actually to execute the command tr 'eai' 'fbj', cut 'iou 'into a length equal to 'fbj', and keep the previous part.
Since there are few options, you can try one by one:
$:~/csat/packages$ tr -d 'ea'i have a good dream.i hv good drm.^C
This option deletes all single occurrences of all characters ('E', 'A') defined in the given character set from the given input, rather than deleting the character combination EA. This is the delete function;
(2) following the Linux Command conventions, any command that can read the input content from the standard input also acts on the output content from the pipeline or the redirected input. Standard input can be used as an interactive tool as a unit test for the tool, and you can verify that you understand it correctly. After ensuring this step, you can use a more practical method. Try:
$ Man tr | tr-D 'ea'
$ Tr 'a-Z' <data.txt> result.txt or cat data.txt | tr 'a-Z'> result.txt
(3) Test-S,-C, option.
$:~/csat/packages$ tr -s 'ea'eeecccaaaeeeecccae
-S is used to replace all repeated characters in a specified character set with a non-repeated one. Consecutive occurrences of characters not defined in the specified character set are not affected. This is a reduction function.
Note that the-C translation is a bit confusing. Let's see what man says:-C,-C, -- complement use the complement of set1. Take the set1 supplement set? The meaning is a bit vague. You can search for it on the Internet and find "use of shell tr command". This is to say: select a character. That is
The part of set1 is not processed, and the rest of the non-conforming part is converted. It is often used with-D and-s. A bit clear. Try:
$ :~ /Csat/packages $ cat a.txt | tr-C 'eaiou' 'H' ahhhehhhihhhhhohhhhhuhhhhhhhh where a.txt contains abcdefghijklmnopqrstuvwxyz
All non-eaiou characters are replaced with H.
Now, the basic meaning and usage of the tr Command Options are clear, and you need to understand the usage scenarios. There are many related articles on the Internet. For more information, see. Of course, you can try to solve the problems raised on the Internet first.
Summary:
1. First, check the help document, which is the most authoritative guidance information;
2. Try this command with no options. Linux commands have been criticized for their wide selection of options. In fact, you may be able to solve many problems by default with no options. The options are just icing on the cake.
3. Read the meaning of the option and practice the option usage. This tests a person's understanding and computer skills. I remember a Daniel said: To program is to understand.
Options are divided into independent options and non-independent options. Most options are independent and can be used and tested separately. Some options are not independent and must be used together with other options;
Options have different functions, some are used to control the processing process, some are used to control the behavior effect, some are used to control the output, and some are used to control the input format. Proper classification is conducive to better understanding of a wide range of options;
Most options are independent and can be used together; first, try to use a single option independently and then try to use it together;
4. Be good at searching and seek answers online. But do not rely on it too much. Otherwise, you cannot improve your ability to independently solve the problem;
5. Chinese documents sometimes do not translate well. It is best to refer to the English document information. Do not avoid learning English. It may determine the basic height of a person's IT career;
6. Most commands accept standard input by default, and can accept pipeline Input and file redirection input. They can be output to standard output by default, or to pipelines and files. The default method can be used for interactive testing tools, while pipelines and redirection are actually practical.
Tr commands represent a large variety of commands, which have fewer options, but are short and practical, and can be understood only with simple computer expertise. Similar Commands include uniq, WC, tail, and head.