Shell Implementation TR Delete replacement detailed _linux shell

Source: Internet
Author: User
Tags character classes control characters lowercase

The TR (translate abbreviation) is used primarily to remove control characters from files or to convert characters.

Syntax: TR [–c/d/s/t] [SET1] [SET2] #SET1: Character Set 1;set2: Character Set 2
-c:complement, replace the characters except SET1 with SET2.
-d:delete, delete all the characters in the SET1, and do not convert.
-s:squeeze-repeats, compresses the duplicated characters in the SET1.
-t:truncate-set1, convert SET1 to SET2, typically by default-T.

1. Remove duplicate characters

#删除空白行就是删除换行符/N.
#注意: There are only carriage returns on these blank lines, no spaces.

$ cat Test.txt

I Love linux!

Hello world!

The Shell is worthy to been studied.

#这里用换行符的转义字符 \ n.
#注意: Here the extra line breaks are removed with-s, and all line breaks are deleted if D is used.

$ Cat Test.txt | tr-s ["\ n"]
I Love linux!
Hello world!
The Shell is worthy to been studied.
#也可以用八进制符 \012,\012 and \ n are line breaks.
$ Cat Test.txt | Tr-s "[\012]"
I Love linux!
Hello world!
The Shell is worthy to been studied.

2, case-by-case interchange

# converts 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
# converts 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

# can also be converted using character classes.
# [: Lower:] stands for lowercase letters, [: Upper:] On behalf of uppercase letters.
$ echo "Hello World I Love You" |tr [: Lower:] [: Upper:]
HELLO World I Love You

3, delete the specified characters

$ cat Test.txt
Monday 09:00
Tuesday 09:10
Wednesday 10:11
Thursday 11:30
Friday 08:00
Saturday 07:40
Sunday 10:00
# Now you want to delete all characters except the processing week.
# d represents deletion, [0-9] represents all numbers, [:] for colons and spaces.
$ Cat Test.txt | Tr-d "[0-9][:]"
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday

4, use-C to make the replacement of the complementary set

# Sometimes in the text we only know some characters to keep, and many other characters, you can use the replacement of the complement.

$ cat Test.txt
Monday 09:00
Tuesday 09:10
Wednesday 10:11
Thursday 11:30
Friday 08:00
Saturday 07:40
Sunday 10:00

# We only need a week, the idea is to replace the letters, everything else.

# here, C: Replaces all characters except the letter with a newline character;-S: Removes extra line breaks.

$ cat Test.txt|tr-cs "[a-z][a-z]" "\ n"
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday

Summary: The conversion of uppercase and lowercase letters, the deletion of unwanted characters is more commonly used. TR syntax is simple and easy to use.

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.