SHELL script-full explanation of tr command usage and features, shell script-tr usage

Source: Internet
Author: User
Tags character classes printable characters

SHELL script-full explanation of tr command usage and features, shell script-tr usage

Directory:
1.1 Introduction
1.2 tr ing
1.3 completely matched replacement
1.4 compressed symbols
1.5 Delete symbols and supplements

1.1 Introduction

Tr is mainly used to map the result set, compress the character, and delete the data read from the standard input. First, it sorts the standard input for reading and then wraps it in a certain way, and then performs Related Processing Based on the given command line parameters.

tr [options] [SET1] [SET2]

-c: Use SET1 completion set
-d: Delete characters
-s: Compressed characters
-t: Truncate SET1 so that the length of SET1 is the same as that of SET2.

1.2 tr ing

If SET1 and SET2 are specified at the same time, the SET1 symbols are mapped to the symbols in set2. In other words, it is the corresponding replacement.

After receiving stdin, tr will first wrap the result according to some mark symbol. For example:

[Root @ xuexi tmp] # ls # Where "one space. log" is the file name with spaces a B c d logdir one space. log shdir sh.txt space. log test vmware-root

Replace spaces with tabs. As Soon As tr receives the data, it performs a new line feed. Therefore, the result only replaces spaces in "one space. log.

[Root @ xuexi tmp] # ls | tr "" \ t "# The result is the abcdlogdironeone space.logshdirsh.txt space. logtestvmware-root

The reason why tr is a ing instead of a replacement is that when the two result sets are replaced, the symbol positions correspond one to one. If SET1 is shorter than SET2, the excess parts of SET2 will be ignored. If SET1 is longer than SET2, POSIX considers this to be unreasonable, but it can also be executed, except that the results are somewhat unexpected. See the following. For example, in the following example, because SET1 only has one symbol "\ n", the Y in SET2 is ignored when it is replaced.

[root@xuexi tmp]# ls | tr "\n" "XY" aXbXcXdXlogdirXoneXone space.logXshdirXsh.txtXspace.logXtestXvmware-rootX

In this way, you can implement simple encryption and decryption.

[Root @ xuexi tmp] # echo "12345" | tr "0-9" "9876543210" # encrypt 87654 [root @ xuexi tmp] # echo "87654" | tr "0- 9 "" 9876543210 "# decrypt 12345

The above process maps the 12345 on the left of the MPs queue to the 0-9 expanded 0123456789, and maps the corresponding bits to the number of set2. The same is true for decryption.

There is a ROT13 encryption algorithm, which uses a set of characters for encryption and decryption. The characters of SET1 are in reverse pairs with the characters of set2. For example, if SET1 specifies the symbol "signature", if SET2 wants to correspond to "opq", it must be extended to "axyopq" and SET2 to "opq.pdf ", the final result is (a, x, y, o, p, q) and (o, p, q, a, x, y), such as (a, o) and (o, a) The matching is successful. Extend it to A-Z and a-z, is the so-called ROT13 encryption, even can add 0-9 and 9-0 corresponding. The following are the mappings between SET1 and set2.

ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm

Encrypt "I love you" now"

[root@xuexi tmp]# echo "I love you" | tr "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" "NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm" V ybir lbh

Decrypt "V ybir lbh.

[root@xuexi tmp]# echo "V ybir lbh" | tr "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" "NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm"I love you

1.3 completely matched replacement

By default, when the specified SET1 is longer than SET2, the remaining characters of SET1 correspond to the last character of set2. If SET1 = [1234], SET2 = [abc], 3 corresponds to c, 4 also corresponds to c. If 3 or 4 appears in the tr operation object, c is replaced.

Using-t, you can first truncate characters longer than SET1, for example, 4, SET1 = [123], and SET2 = [abc.

[Root @ xuexi tmp] # cat x.txt NO Name SubjectID Mark remarks 1 longshuai 001 56 fail 2 gaoxiaofang 001 60 pass 3 zhangsan 001 50 fail 4 lisi 001 80 pass 5 wangwu 001 90 pass [root @ xuexi tmp] # cat x.txt | tr "fang" "jin" # both n and g are replaced with nNO Nime SubjectID Mirk Note 1 lonnshuii 001 56 fail 2 nioxiiojinn 001 60 pass 3 zhinnsin 001 50 fail 4 lisi 001 80 pass 5 winnwu 001 90 pass [root @ xuexi tmp] # cat x.txt | tr-t "fang" jin "# g is truncated, only Replace the fan with jinNO Nime SubjectID Mirk remarks 1 longshuii 001 56 fail 2 gioxiiojing 001 60 pass 3 zhingsin 001 50 fail 4 lisi 001 80 pass 5 wingwu 001 90 pass

1.4 compressed symbols

This feature is amazing.

tr -s [SET1] [SET2]

If SET2 is not specified, only compression is performed and no replacement is performed. SET1 can specify multiple characters to compress each character. For exampletr -s "0a"That is, it will compress the continuous 0, and also compress the continuous. If SET2 is specified, it is replaced one by one after compression.

For example, the content in the x.txt file is as follows. There are many spaces and few spaces. That is to say, this is an unformatted file.

[Root @ xuexi tmp] # cat x.txt NO Name SubjectID Mark remarks 1 longshuai 001 56 fail 2 gaoxiaofang 001 60 pass 3 zhangsan 001 50 fail 4 lisi 001 80 pass 5 wangwu 001 90 pass

Use tr to compress spaces to change the rule.

[Root @ xuexi tmp] # cat x.txt | tr-s "" NO Name SubjectID Mark remarks 1 longshuai 001 56 fail 2 gaoxiaofang 001 60 pass 3 zhangsan 001 50 fail 4 lisi 001 80 pass 5 wangwu 001 90 pass

If SET2 is specified, replace it "-".

[Root @ xuexi tmp] # cat x.txt | tr-s ""-"NO-Name-SubjectID-Mark-remarks 1-longshuai-001-56-fail Failover-pass 3-zhangsan-001-50-fail 4-lisi-001-80- pass 5-wangwu-001-90-pass

1.5 Delete symbols and supplements

tr -dIs to delete the specified symbol, only one SET1 can be connected.

[Root @ xuexi tmp] # cat x.txt | tr-d "" NONameSubjectIDMark remarks 1longshuai00156 fail 2gaoxiaofang00160 pass 3zhangsan00150 fail 4lisi00180 pass 5wangwu00190 pass

tr -c SET1 SET2Is to calculate the complement set for the standard input according to SET1, and replace all the characters in the complement set with SET2, that is, the character that does not exist in the standard input but does not exist in SET1 is replaced with SET2. However, if the value of SET2 is greater than 1, only the last character is used as the replacement character. When using-c, you should take-c SET1 as a whole and do not separate it.

For example:

[root@xuexi tmp]# echo "abcdefo"| tr -c "ao" "y"ayyyyyoy[root@xuexi tmp]# 

The complement set obtained by the standard input "abcdefo" according to SET1 = "ao" is bcdef, and they are replaced with y. The result is ayyyyyo, however, there is an additional y at the end of the result and the command prompt is followed. This is because \ n at the end of abcdefo is also a part of ao's complementary set and replaces it with y. If you do not want to replace the last \ n, you can specify \ n in SET1.

[root@xuexi tmp]# echo "abcdefo"| tr -c "ao\n" "y" ayyyyyo

If SET2 specifies multiple characters, only the last character is used as the replacement character.

[root@xuexi tmp]# echo "abcdefo"| tr -c "ao\n" "ay"ayyyyyo[root@xuexi tmp]# echo "abcdefo"| tr -c "ao\n" "yb"abbbbbo

"-C" is often used with "-d", suchtr -d -c SET1. It first runs "-c SET1" to find the set of SET1, and then deletes the set. That is to say, the final result is a full match of characters in set1. Note that "-d" must be placed before "-c", otherwise it will be parsedtr -c SET1 SET2Instead of deleting the complete set, replace the last character "-d" with d.

[Root @ xuexi tmp] # echo "one 1 two 2 three 3" | tr-d-c "[0-9] \ n" # Calculate the supplement set for numbers and line breaks, and delete these collection symbols 123 [root @ xuexi tmp] # echo "one 1 two 2 three 3" | tr-d-c "[0-9] \ n" # Add one space complement 1 2 3 [root @ xuexi tmp] # echo "one 1 two 2 three 3" | tr-c "[0-9] \ n"-d #-d option after-c option is replaced by dddd1ddddd2ddddddd3 [root @ xuexi tmp] # echo "one 1 two 2 three 3" | tr-d-c "[a-zA-z] \ n "# reserved letter onetwothree [root @ xuexi tmp] # echo" one 1 two 2 three 3 "| tr-d-c" [a-zA-z] \ n "# retain letters with spaces one two three

From the experiment of the complementary set above, we can see that the specified [0-9] and [a-z] are actually character classes, and the final result is the objects in this class.

You can use the following character classes in tr. These classes can also be used in some other commands.

[:alnum:]All numbers and letters.
[:alpha:]All letters.
[:blank:]All horizontal spaces = spaces + tab.
[:cntrl:]For all control characters (non-printable characters), The octal character 0-37 in the ascii table corresponds to the character and 177 del.
[:digit:]All numbers.
[:graph:]All printed characters, excluding spaces = numbers + letters + punctuation.
[:lower:]All lowercase letters.
[:print:]All printed characters, including spaces = numbers + letters + punctuation + spaces.
[:punct:]All punctuation marks.
[:space:]All horizontal or vertical spaces = space + tab + line break + vertical tab + Page Break + enter key.
[:upper:]All uppercase letters.
[:xdigit:]All hexadecimal numbers.

The usage method is as follows. For example, [: upper:] is equivalent to [A-Z], [: digit:] is equivalent to [0-9].

[root@xuexi tmp]# echo "one ONE 1 two TWO 2 three THREE 3" | tr -d -c "[:upper:] \n" ONE   TWO   THREE [root@xuexi tmp]# echo "one ONE 1 two TWO 2 three THREE 3" | tr -d -c "[:alpha:] \n"one ONE  two TWO  three THREE [root@xuexi tmp]# echo "one ONE 1 two TWO 2 three THREE 3" | tr -d -c "[:digit:] \n"  1   2   3

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

Reprinted please indicate the source: Success!

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.