The TR (translate abbreviation) is primarily used to remove control characters from a file, 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 characters in SET1, do not convert.
-s:squeeze-repeats, compresses the repeated characters in the SET1.
-t:truncate-set1, the SET1 is converted with SET2, the general default is-T.
1. Remove duplicate characters
#删除空白行就是删除换行符/N.
#注意: There are only carriage returns on these blank lines, and there are no whitespace characters.
$ cat Test.txt
I Love linux!
Hello world!
Shell is worthy to been studied.
#这里用换行符的转义字符 \ n.
#注意: The extra line break is removed here with-s and all newline characters are removed if you use-D.
$ Cat Test.txt | tr-s ["\ n"]
I Love linux!
Hello world!
Shell is worthy to been studied.
#也可以用八进制符 \012,\012 and \ n are newline characters.
$ Cat Test.txt | Tr-s "[\012]"
I Love linux!
Hello world!
Shell is worthy to been studied.
2, the case of the interchange
# converts all lowercase letters in the statement to uppercase, where-t can be omitted.
$ echo "Hello World I Love You" |tr [-t] [A-z] [a-z]
HELLO World I Love You
# turn 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
# You can also use the character class to convert.
# [: Lower:] represents lowercase letters, [: Upper:] represents uppercase letters.
$ echo "Hello World I Love You" |tr [: Lower:] [: Upper:]
HELLO World I Love You
3. Delete the specified character
$ 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 that are processed outside of week.
#-D for Delete, [0-9] for all numbers, [:] to represent colons and spaces.
$ Cat Test.txt | Tr-d "[0-9][:]"
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
4. Replace with-c for complement set
# Sometimes in the text we know only some of the characters to keep, and the other characters are a lot more, you can use the substitution 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, then the idea is to replace all the other words except the letters.
# here,-C: Replace all characters except the letter with a newline character;-s: Removes extra newline characters.
$ cat Test.txt|tr-cs "[a-z][a-z]" "\ n"
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
Summary: where uppercase and lowercase letters are converted, deleting unwanted characters is more commonly used. TR syntax is simple and easy to use.
# # # # #2013.12.23.shenweiyan######
TR Removal and replacement explanation