Shell Script--tr command usage and feature full solution

Source: Internet
Author: User
Tags control characters diff sorts

This article directory:
1.1 Introduction
1.2 TR Mapping
1.3 Completely corresponding replacement
1.4 Compression Symbols
1.5 deleting symbols and complement sets

1.1 Introduction

TR is primarily used for result set mapping, character compression, and character deletion of data read from standard input. It first sorts the standard input that is read and then wraps it in some way, and then handles it according to the command-line arguments given.

tr[options][SET1][SET2]

-c: Using the complement of SET1
-d: Delete character
-s: Compressed characters
-t: truncates the SET1 so that the length of the SET1 is the same as the length of the SET2

1.2 TR Mapping

If both SET1 and SET2 are specified, the symbol for SET1 is mapped to the symbol in SET2 by position one by one. In other words, it is the corresponding substitution.

When TR receives the stdin, it first wraps the result according to a marker symbol. For example:

[root@xuexi tmp]# ls              # 其中"one space.log"是带有空格的文件名a  b  c  d  logdir  one  one space.log  shdir  sh.txt  space.log  test  vmware-root

Replaces a space with a tab character. Because the TR receives the data and sorts the lines, the result only replaces the space in "one Space.log".

[root@xuexi tmp]# ls | tr " " "\t"     # 结果是排序后换行的abcdlogdironeone    space.logshdirsh.txtspace.logtestvmware-root

The reason is that TR is a mapping rather than a substitution, because the symbol position is one by one corresponding to the two result set substitution. If the SET1 is shorter than the SET2, then the SET2 superfluous part is ignored, and if SET1 is longer than SET2, POSIX thinks it is unreasonable but can execute, but the result is somewhat unexpected, see below. For example, because there is only one symbol "\ n" in SET1, then the y in SET2 is ignored when replaced.

[root@xuexi"\n""XY" aXbXcXdXlogdirXoneXone space.logXshdirXsh.txtXspace.logXtestXvmware-rootX

This allows for simple encryption and decryption.

[root@xuexi"12345""0-9""9876543210"     # 加密87654[root@xuexi"87654""0-9""9876543210"     # 解密12345

The above procedure corresponds to the 12345 on the left side of the pipeline to 0-9 of the expanded 0123456789, and maps the corresponding bit to the number of SET2. Decryption is also the same.

There is a ROT13 encryption algorithm, which encrypts and decrypts the use of a set of characters. Its SET1 letter bit and SET2 's letter bit completely reverse into pairs. For example SET1 the specified symbol is "Axy", if SET2 want to correspond to "OPQ", then SET1 should be extended to "AXYOPQ", SET2 extension to "Opqaxy", eventually (A,x,y,o,p,q) and (O,p,q,a,x,y), so (A, O) and (O,a) can be paired successfully. Extend it to A-Z and A-Z, which is called ROT13 encryption, and can even add 0-9 to the 9-0 counterpart. The following are the corresponding SET1 and SET2.

ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm

Now encrypt "I love You"

[root@xuexi"I love you""ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz""NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm" V ybir lbh

Decrypt "V Ybir lbh".

[root@xuexi"V ybir lbh""ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz""NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm"I love you

1.3 Completely corresponding replacement

By default, when the specified SET1 is longer than the SET2 character, the last corresponding position is SET1, and the remaining word nonalphanumeric and SET2 are corresponding. If SET1=[1234],SET2=[ABC], then 3 corresponds to c,4 also corresponds to C, at this time if the TR operation of the object appears 3 or 4 will be replaced by C.

You can use-T to truncate SET1 characters longer than SET2, such as truncating redundant 4,set1=[123] and SET2=[ABC] to achieve full correspondence.

[[email protected] tmp]# cat X.txtNOName Subjectid Mark Remarks1Longshuai001   AboutFail to pass2Gaoxiaofang001  -Pass3Zhangsan001  -Fail to pass4Lisi001    thePass5Wangwu001    -pass [[email protected] tmp]# cat X.txt | tr "Fang " "Jin" # results in N and g are replaced by nNONime Subjectid Mirk Notes1Lonnshuii001   AboutFail to pass2Nioxiiojinn001  -Pass3Zhinnsin001  -Fail to pass4Lisi001    thePass5Winnwu001    -pass [[email protected] tmp]# cat X.txt | tr-t "Fang " "Jin" # G is truncated, only corresponds to the replacement fan for JinNONime Subjectid Mirk Notes1Longshuii001   AboutFail to pass2Gioxiiojing001  -Pass3Zhingsin001  -Fail to pass4Lisi001    thePass5Wingwu001    -Pass

1.4 Compression Symbols

This feature is so cool.

tr[SET1][SET2]

If you do not specify SET2, only the compression is not replaced. SET1 can specify more than one character, which compresses each character, for example, it compresses tr -s "0a" a continuous 0, and it compresses the continuous a. If SET2 is specified, it is also replaced with a corresponding replacement after compression.

If the contents of the X.txt file are as follows, there are many places in the space, there are few places, that is, this is a file without formatting.

[root@xuexi tmp]# cat x.txtNO Name SubjectID Mark 备注1  001  56 不及格2  gaoxiaofang  00160 及格3  00150 不及格4  lisi    001   80 及格5  wangwu   001   90 及格

A rule that uses TR to compress spaces to make them change.

[[email protected] tmp]# cat x.txt | tr -s " "NO Name SubjectID Mark 备注100156 不及格200160 及格300150 不及格400180 及格500190 及格

If SET2 is specified, replace with "-".

[[email protected] tmp]# cat x.txt | tr -s " " "-" NO-Name-SubjectID-Mark-备注1-longshuai-001-56-不及格2-gaoxiaofang-001-60-及格3-zhangsan-001-50-不及格4-lisi-001-80-及格5-wangwu-001-90-及格

1.5 deleting symbols and complement sets

tr -dis to delete the specified symbol, which can only be followed by a SET1.

[[email protected] tmp]# cat x.txt | tr -d " "NONameSubjectIDMark备注1longshuai00156不及格2gaoxiaofang00160及格3zhangsan00150不及格4lisi00180及格5wangwu00190及格

tr -c SET1 SET2is to SET1 the standard input by the complement set and replace all the characters of the complement portion with SET2, replacing characters that are not present in the standard input but that do not exist in SET1 with SET2 characters. But SET2 if the specified character is greater than 1, only the last character is taken as the replacement character. Use-c should be used when the-C SET1 as a whole, do not separate it.

For example:

[root@xuexi"abcdefo""ao""y"ayyyyyoy[root@xuexi

The standard input "Abcdefo" is bcdef with a complement of set1= "AO", replacing them with y, and the result is ayyyyyo, but the result is more than one Y and immediately following the command prompt. This is because the ABCDEFO tail is also part of the AO's complement and replaces it with Y. If you do not want to replace the last \ n, you can specify \ n in SET1.

[root@xuexi"abcdefo""ao\n""y" ayyyyyo

If SET2 specifies more than one character, only the last character is taken as the replacement character.

[root@xuexi"abcdefo""ao\n""ay"ayyyyyo[root@xuexi"abcdefo""ao\n""yb"abbbbbo

"-C" is often used with "-D", such as tr -d -c SET1 . It first executes "-C SET1" to find the complement of SET1, and then perform the deletion of the complement. That is, the final result is to exactly match the characters in the SET1. Note that the "-D" must precede "-C", otherwise it is resolved to tr -c SET1 SET2 do not delete the complement, but instead replace the complement with the last character d of "-D".

[Root@xuexitmp]# Echo"One 1, 2 three 3"| Tr-d-C"[0-9]\n]# Fu Tiu a complement to numbers and branches, and delete these complement symbols123[Root@xuexitmp]# Echo"One 1, 2 three 3"| Tr-d-C"[0-9] \ n"# Add a space to complement the set1  2  3[Root@xuexitmp]# Echo"One 1, 2 three 3"| Tr-c"[0-9]\n]The-D #-D option is followed by the replace behavior after the-C option dddd1ddddd2ddddddd3[root@xuexitmp]# Echo"One 1, 2 three 3"| Tr-d-C"[a-za-z]\n]# Reserved Letters Onetwothree[root@xuexitmp]# Echo"One 1, 2 three 3"| Tr-d-C"[a-za-z] \ n"# Keep The letters while preserving the space one and one of the three

As can be seen from the experiment of the complement set above, the specified [0-9] and [A-z] are a character class, and the final result shows the object in this class.

The following types of characters are available in tr. These classes can also be used in some other commands.

[:alnum:]All the numbers and letters.
[:alpha:]All the letters.
[:blank:]All horizontal blanks = Space +tab.
[:cntrl:]All control characters (nonprinting characters), the octal 0-37 corresponding characters in the ASCII table, and the 177 del.
[:digit:]All numbers.
[:graph:]All printed characters, not including spaces = numbers + letters + punctuation.
[:lower:]All lowercase letters.
[:print:]All printed characters, including spaces = numbers + letters + punctuation + spaces.
[:punct:]All punctuation.
[:space:]All horizontal or vertical blanks = spaces +tab+ line breaks + Vertical tab+ page break + ENTER.
[:upper:]All uppercase letters.
[:xdigit:]all hexadecimal digits.

Use methods such as the following. For example [: Upper:] is equivalent to [a-z],[:d Igit:] equivalent to [0-9].

[root@xuexi"one ONE 1 two TWO 2 three THREE 3""[:upper:] \n" ONE   TWO   THREE [root@xuexi"one ONE 1 two TWO 2 three THREE 3""[:alpha:] \n"one ONE  two TWO  three THREE [root@xuexi"one ONE 1 two TWO 2 three THREE 3""[:digit:] \n"  1   2   3

Back to series article outline:http://www.cnblogs.com/f-ck-need-u/p/7048359.html

Reprint Please specify source:http://www.cnblogs.com/f-ck-need-u/p/7521506.html Note: If you think this article is not bad please click on the lower right corner of the recommendation, with your support to inspire the author more enthusiasm for writing, thank you very much!

Shell Script--tr command usage and feature full solution

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.