The shell Script TR command is used

Source: Internet
Author: User
Tags character classes

The TR command is used to replace the contents of the standard input. For example

# echo ' HELLO World!!! ' | TR "A-Z" "A-Z"
Hello World!!!

Here the "A-Z", "A-Z" all represent the collection, the shell script defines the collection type is very simple, that is, the specified collection sequence, but for the upper case, must not enter all the collection type, can be defined by the "Start character-end character" method of the collection. Using the TR command in conjunction with a collection can solve many complex problems.

Use TR for data encryption, decryption:

# echo 456 | TR "0-9" "9876543210"
543

# echo 543 | TR "9876543210" "0-9"
55W

TR for character substitution:

# Cat Dept

Ten ACCOUNTING NEW YORK
DALLAS
SALES CHICAGO
OPERATIONS BOSTON

# Cat Dept | TR "\ T" "" "

Ten ACCOUNTING NEW YORK
DALLAS
SALES CHICAGO
OPERATIONS BOSTON

Specify the-d parameter to delete the specified string:

# echo ' Hello 123 world 456!!! ' | Tr-d ' {0-9} '
Hello World!!!

Specify the-c parameter for complement processing, usually with-D, to delete characters that do not meet the criteria:

# echo ' Hello 123 world 456!!! ' | Tr-d-c ' {0-9 \ n} '
123 456

The-s parameter in the TR command compresses characters, compressing successive repeating characters into characters, and the most common scenario is to compress the blank lattice:

# echo ' Hello 123 world 456!!! ' | Tr-s "
Hello 123 World 456!!!

Tip: Use TR to add the list of characters in the file:

# seq 5 | Echo $[$ (tr ' \ n ' + ') 0]
15

Explanation: After replacing \ n with +, the script output becomes $[1+2+3+4+5+0], which can be calculated directly, eliminating the hassle of looping the numbers.

TR can also use a variety of different character classes like a collection.

The shell script's TR command is used

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.