Usage of shell study... tr

Source: Internet
Author: User

Generally, we use tr to replace strings or delete specified strings.

The TR syntax is as follows:

Tr [Option]... set1 [set2]

-C,-C, -- Complement

First complement set1

Replace the characters not in set1 with set2


[[email protected] ~]$ echo "lubinsu" | tr -c "l" "A"lAAAAAAA

-D, -- delete
Delete characters in set1, do not translate

Delete characters that contain set1:

[[email protected] ~]$ echo "lubinsu" | tr -d "u"lbins


[[email protected] ~]$ echo "lu123b123ins41u" | tr -d "0-9"lubinsu

-S, -- Squeeze-repeats replace each input sequence of a repeated character that is listed in set1 with a single
Occurrence of that character

Remove duplicate characters and compress them into one character:

[[email protected] ~]$ echo "lubinsu" | tr -c "l\n" "A"lAAAAAA[[email protected] ~]$ echo "lubinsu" | tr -cs "l\n" "A"lA

-T, -- truncate-set1

First truncate set1 to length of set2

The default value is-T:

[[email protected] ~]$ echo "lubinsu" | tr -t "lu" "abc"abbinsb[[email protected] ~]$ echo "lubinsu" | tr "lu" "abc"abbinsb

Other examples:

Case-insensitive replacement:

[[email protected] ~]$ echo "lubinsu" | tr "a-z" "A-Z"LUBINSU[[email protected] ~]$ echo "lubinsu" | tr [:lower:] [:upper:]LUBINSU



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.