Linux Shell and TR commands

Source: Internet
Author: User

1. Description

Tr-translate or delete characters

Tr [Option]... set1 [set2]

Translate, squeeze, and/or delete characters from standard input, writing to standard output. Sets are specified as strings of characters.

TR is used to convert characters from standard input through replacement or deletion. Use TrProvide two strings: Set1Used for query, Set2Used to process the conversion.TrExecution time,String1The characters in are mapped to strings.2Characters in, Then perform the conversion operation.


-C requires the character set to ASCII . Use set2 replace set1 use set2 the character replacement is not in set1 character

-D DeleteSet1All input characters.

-S deletes all recurring character sequences and only keeps the first

Translation occurs if-D is not given and both set1 and set2 appear.

3. character range

[A-Z]: [A-Z]: [0-9]: digit string

\ Octal: octal number of three digits,Corresponding validASCIICharacter

[Char * repeat] Repeat copies of char, repeat octal if starting with 0

4. Example

1) Remove duplicate characters

Tr-s "[A-Z]" <example.txt

Cat example.txt | tr-s "[A-Z]"

2) Delete empty rows

Cat example.txt | tr-s "[\ n]"

3) Small write in upper case

Cat example.txt | TR "[A-Z]" "[A-Z]"

Cat example.txt | TR "[: lower:]" [: Upper:]"

4) replace

CAT/etc/passwd | tr-s "[:]" [\ t]"

5)Tr-s "[\ n]" <Test> test11 //Input file isTest,The output file isTest1

6)Cat test.txt | tr-d "[0-9] [:]" //Delete numeric characters and colons

7) CAT test.txt | tr-CS" [A-Z] [A-Z] "\ n" //-C : replace all characters except letters with line breaks; -S : delete redundant linefeeds

5. PassTrAll functions can be implemented throughSed. Converts uppercase and lowercase letters, and removes unnecessary characters.


[ 1 ] Uid = 1813014 & Do = Blog & cuid = 364891

[ 2 ]


Http:// Uid = 1813014 & Do = Blog & cuid = 364891

[ 4 ] OS /201109/104590.html

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: 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.