Linux TR Command

Source: Internet
Author: User

kuang:~$ TR--helpusage:tr [OPTION] ... SET1 [SET2] Translate, Squeeze, and/or Delete characters fromStandard input,writing to standard output. -C,-C,--complement Use the complement of SET1-D,--delete delete charactersinchSET1, DoNot translate-S,--squeeze-repeats replace each input sequence of a repeated character that isListedinchSET1 with a single occurrence of that character-T,--truncate-Set1 First truncate SET1 to length of SET2--help Display ThisHelp and exit--version output version information and exitsets are specified asStrings of characters. Most represent themselves.

1. No additional options:

echo "Hello World" | Tr ' Hello ' ' 12345 '

Output: 12445 w5r4d

Explanation: Here TR does the substitution operation, SET1 is "Hello", SET2 is "12345"

Building a mapping Relationship: H-1

E-2

L-3

L -, 4 */L letters mapped two times, the last time after the previous * /

O-5

So: The output is 12445 w5r4d

What if the length of the SET1 at this time > SET2?

For example: echo "Hello World" | Tr ' hellowd ' 12345 '

Output: 12445 55r45

Both W and D are mapped to 5, so be clear ....

What if the length of the SET1 at this time < SET2?

For example: echo "Hello World" | Tr ' He ' 12345 '

Output: 12llo World

SET2, the extra-long part is useless.

2.-T option

For example: echo "Hello World" | Tr ' hellowd ' 12345 '

Output: 12445 55r45

Another example: echo "Hello World" | TR- t ' hellowd ' 12345 '

Output: 12445 w5r4D

Explanation:-T first truncate SET1 to length of SET2

First cut the length of the SET1 to SET2 length, then set up the mapping, and then replace ...

3.-C option

echo "Hello World" | Tr-c ' [o-q] ' 1 '

Output: 1111o 1o111

Explanation: The-C option is to take complement set, complete with ASCII characters (not sure, haha ...). )

-C ' [o-q] ': equivalent to SET1 equals OPQ three letters outside of ASCII characters

Then you understand ...

4.-D option

echo "Hello World" | Tr-d ' Hel '

Output: o Word

Explanation:-D is to erase the characters that appear in the SET1

5.-S option

echo "Hello World" | Tr-s ' Hel '

Output: Helo Word

Explanation:-S is the character that will appear in the SET1 and is pressed when repeated.

This example is: if more than one h appears together, squeeze into an H ...

If more than one E appears together, squeeze into an e ...

If more than one L is present together, squeeze into an l ...

Linux TR Command

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.