Tr command for Shell Learning

Source: Internet
Author: User
Tags first string

The TR command does not accept specified file parameters, but only translates standard input. tr is short for translate, that is, translation. Note that it cannot translate sentences, only one character can be translated.

First, define the variable:

    [[email protected]]#A=1,,2,,,3,,,4,,,5

The following example describes the command and its common options.

Example:

1. No option settings

    [[email protected]]# echo $A | tr  ‘,‘ ‘ ‘        1 2   3   4   5    [[email protected]]# echo $A | tr [2-4] [b-d]    1,,b,,,c,,,d,,,5    [[email protected]]# echo $A | tr "23""bc"    1,,b,,,c,,,4,,,5

Note: here we will not replace the string, but Replace "2" with "B" and "3" with "C ".

Frequently used: lower case --> upper case

    # tr [a-z] [A-Z]

2. Select "-s" to delete all recurring character sequences. Only the first string is retained, and the recurring string is compressed into a string.

    [[email protected]]#echo $A | tr -s ‘,‘        1,2,3,4,5    [[email protected]]#echo $A | tr -s ‘,‘ ‘ ‘    12 3 4 5

3. Select "-d" to delete all input characters specified by the string.

    [[email protected]]#echo $A | tr -d "23"        1,,,,,,,,4,,,5    [[email protected]]#echo $A | tr -d  ‘,‘    12345

Frequently used: Delete the '^ M' character in a Windows File

    # tr -d"/r"

4. The option "-c" is used to replace the character set in the string. the character set must be a s c I.

    [[email protected]]# echo $A | tr -c  [1-5] ‘%’        1%%2%%%3%%%4%%%5


This article is from the "Linux" blog, please be sure to keep this source http://4983206.blog.51cto.com/4973206/1438109

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.