Linux command learning example: tr

Source: Internet
Author: User
Linux command learning example: tr briefly introduces some basic methods and precautions for learning Linux commands in "How to learn linux commands and tools" in the blog "how to learn Linux Commands. This article will take some commands as an example to talk about how to learn a specific linux command. Note: This article... information & nb Linux command learning example: tr briefly talked about some basic methods and precautions for learning Linux commands in "How to learn linux commands and tools" in the blog. This article will take some commands as an example to talk about how to learn a specific linux command. Note: the focus of this article is not on the command itself, but on the command learning method. It is better to give a basket of fish than to teach people a set of effective fishing methods. Example 1: tr (1) first view the help information, tr -- help or man tr: [plain] 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 the version information and exits it is known 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: www.2cto.com [plain] $ :~ /Csat/packages $ tr 'eaiou' 'fbjpv' I have a dream j hbvf B drfbm can be seen in the result that tr is used for character conversion (in character conversion unit, instead of character sequence), replace all occurrences of the characters defined in the character set specified earlier with the occurrence of the characters corresponding to the position in the character set specified later. It looks like it can be used as a small encryption method. Try the case where the position length does not match: a. SET1 length is smaller than SET2: It seems that it does not affect [plain] $ :~ /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 SET1 position is replaced with the appearance of the last character of SET2 [plain] $ :~ /Csat/packages $ tr 'eaiou' 'fbjp 'I love you, u know? J lpvf ypp, p knpw? O and u are replaced with p. -T option seems to be somewhat correlated. Try: [plain] $ :~ /Csat/packages $ tr-t 'eaiou' 'fbj' I love you j lovf you now o, 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. Because there are few options, you can try one by one: [plain] $ :~ /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; www.2cto.com (2) follows the Linux command convention, and any command that can read the input content from the standard input also acts on the output content from the pipeline or the redirection 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' 'a-Z' <data.txt> result.txt or cat data.txt | tr 'A-z'' a-z '> result.txt (3) test-s,-c, option. [Plain] $ :~ /Csat/packages $ tr-s 'EA 'eeecccaaaeee ecccae-s: if the characters defined in the specified character set repeatedly appear in the input, replace all of its repeated occurrences with a non-repeated occurrence. 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 conforming to 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: [plain] $ :~ /Csat/packages $ cat a.txt | tr-c 'eaiou' 'H' aHHHeHHHiHHHHHoHHHHHuHHHHHH. a.txt contains abcdefghijklmnopqrstuvwxyz and 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. To sum up, 1. first view the help document, which is the most authoritative guidance information; 2. First, use the command without any 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: the To program is to understand. www.2cto.com option is 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, and 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 helps you better understand a wide range of options. most options are independent and can be used together. try to use them independently and then. 4. be good at searching and seek answers online. But do not rely 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 English documents. 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.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.