Learn a little shell every day -- tr

Source: Internet
Author: User
Tags character classes control characters

TR is used to delete control characters in a file or convert characters.
Syntax: TR [-C/D/S/T] [set1] [set2]
Set1: Character Set 1
Set2: Character Set 2
-C: complement. Use set2 to replace characters not included in set1.
-D: delete all characters in set1, without conversion
-S: Squeeze-repeats, compresses repeated characters in set1
-T: truncate-set1, which converts set1 to set2. The default value is-t.
 
1. Remove repeated characters
# Compressing several consecutive identical characters into one character
$ Echo aaacccddd | tr-s [A-Z]
ACD
$ Echo aaacccddd | tr-s [ABC]
Acddd
 
2. Delete blank rows
# Deleting blank rows is to delete linefeeds/n
# Note: There are only carriage return characters and no space characters on these blank lines.
$ Cat test.txt
I love Linux!


Hello world!

Shell is worthy to been studied
 
# Use the Escape Character \ n of the line break here
# Note: extra line breaks are deleted with-S. If-D is used, all line breaks are deleted.
$ Cat test.txt | tr-s ["\ n"]
I love Linux!
Hello world!
Shell is worthy to been studied
# You can also use the octal character \ 012, \ 012, and \ n as line breaks.
$ Cat test.txt | tr-s "[\ 012]"
I love Linux!
Hello world!
Shell is worthy to been studied
 
3. case-insensitive Conversion
# Convert all lowercase letters in the statement into uppercase letters, where-t can be omitted
$ Echo "Hello world I Love You" | tr [-T] [A-Z] [A-Z]
Hello world I love you
# Convert all uppercase letters in the statement into lowercase letters
$ Echo "Hello world I Love You" | tr [A-Z] [A-Z]
Hello world I love you
# Conversion using character classes
# [: Lower:] indicates lower-case letters, and [: Upper:] indicates upper-case letters.
$ Echo "Hello world I Love You" | tr [: lower:] [: Upper:]
Hello world I love you
 
4. Delete the specified character
$ Cat test.txt
Monday
Tuesday
Wednesday 10: 11
Thursday 11: 30
Friday
Saturday
Sunday
# Delete All characters outside the processing week
#-D Indicates deletion, [0-9] indicates all numbers, and [:] indicates colons and spaces.
$ Cat test.txt | tr-d "[0-9] [:]"
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
 
5. Use-C to replace the complementary set
# Sometimes we only know the characters to be retained in the text. If there are many other types of characters, we can replace them with supplements.
$ Cat test.txt
Monday
Tuesday
Wednesday 10: 11
Thursday 11: 30
Friday
Saturday
Sunday
# If we only need a week, the idea is to replace all the letters except letters.
# Here,-C: replace all characters except letters with line breaks;-S: Delete unnecessary line breaks
Cat test.txt | tr-CS "[A-Z] [A-Z]" \ n"
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
 
Conclusion: the conversion of uppercase and lowercase letters is commonly used to delete unnecessary characters. Tr syntax is simple and easy to use.
Author: "to_be_monster_of_it"

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.