Basic tutorial of TR command in Linux system

Time:2020-3-29

Usage: tr [options]… Set1 [set2]
Replace, reduce, and / or delete characters from standard input and write the results to standard output.

– C, – C, – completion first make up Set1
– D, — delete delete the content matching Set1 without replacing it
– s, — squeeze repeats if the characters matching Set1 are continuous in the input sequence
Repeat. It will be reduced to one character in replacement
– t, — truncate-set1 first truncate the length of Set1 to be equal to set2
— help display this help message and exit
— version displays version information and exits

Set is a set of strings, which can be understood literally. The analytical sequence is as follows:

\ NNN characters (1 to 3 digits) with an octal value of NNN
\ \ backslash
\ a terminal rings
\ B backspace
\ f page feed
\ N line feed
\ r enter
\ tlevel tab
\ V vertical tab
Character 1-character 2 all characters experienced during the ascending increment from character 1 to character 2
[character *] applies in set2. The specified character will be copied continuously until it matches the length of setting 1
[number of characters * times] copies the characters for a specified number of times. If the number of times starts with 0, it is regarded as an octal number
[: alnum:] all letters and numbers
[: Alpha:] all letters
[: Blank:] all white space characters arranged horizontally
[: CNTRL:] all control characters
[: digit:] all numbers
[: graph:] all printable characters, excluding spaces
[: lower:] all lowercase letters
[: print:] all printable characters, including spaces
[: punct:] all punctuation characters
[: Space:] all white space characters arranged horizontally or vertically
[: Upper:] all uppercase letters
[: xdigit:] all hexadecimal numbers
[= character =] all characters equal to the specified character

Only when Set1 and set2 are given and there is no – D option at the same time will the replacement be performed.
The – t option is only possible when replacing. If you need set2, it will be added by adding the original end character at the end
Add to the same length as Set1. Extra characters in set2 will be omitted. Only [: lower:] and [: Upper:]
Expand characters in ascending order; in set2 when used for substitution, case conversion is represented in pairs. -S acts on Set1, neither
The replacement is not deleted, otherwise set2 reduction is used after replacement or expansion.

The parameter – C in the TR command is more practical. You can replace this character set with the complement of the character set in string 1. The character set is required to be a s c I. Then a command is tried on the terminal:

Copy code

The code is as follows:

tr -cs “[a-z][A-Z]” “[\012*]” <diary.txt

This is to convert the non character data in the file diary.txt into line breaks, and then use the – s option to delete all repeated character sequences, leaving only the first one; that is, to compress the repeated character strings into one string.

Later, when I looked at this command carefully, I found that a * was added after \ 012. When I removed this * and ran it again, I found that the results were all on the same line and separated by (left bracket). Then I tried to use \ n instead of \ 012,
It is also found that the correct result can only be obtained by adding * after \ n. The brackets were later removed. This is to output correct results when * is not required. When * is issued, all results will be output in one line, separated by * in the middle.

Conjecture: whether or not after the brackets, the special characters inside should be followed by *, whether in letters or octal numbers.

Here are some more examples:

Copy code

The code is as follows:

[root @ localhost Zhangy] ᦇ echo “tank” | tr A-Z A-Z ᦇ uppercase to lowercase

tank

Copy code

The code is as follows:

[root @ localhost Zhangy] ᦇ echo ‘tank Zhang’ | tr A-Z A-Z ᦇ lowercase to uppercase

TANK ZHANG

Copy code

The code is as follows:

[root @ localhost Zhangy] ා cat aaa.txt ා original file

aaa

bbb

Copy code

The code is as follows:

[root @ localhost Zhangy] ා cat a a a.txt| tr ‘a”c ‘ා letter C replaces letter a

ccc

bbb

Copy code

The code is as follows:

[root @ localhost Zhangy] ා cat a a a.txt| tr – D ‘a’ ා delete all letters a

bbb

Copy code

The code is as follows:

[root @ localhost Zhangy] ා cat aaa.txt| tr – D ‘\ n \ t’ delete the line feed ‘\ n’, tabulation ‘\ t’ characters in the file file

aaabbb

Copy code

The code is as follows:

[root @ localhost Zhangy] ා cat a a a.txt| tr – s [a-za-z] ා delete duplicate letters

a

b

Copy code

The code is as follows:

[root @ localhost Zhangy] ා cat aaa.txt| tr – s’ \ n ‘ා delete blank lines

aaa
bbb

Copy code

The code is as follows:

[root @ localhost Zhangy] ා cat aaa.txt| tr – s’ \ 011 ‘\ 040’ ා replace tab with space character \ 040 \ 011

aaa

bbb

Copy code

The code is as follows:

#Tr a C < test ා change a in the test file to C