Basic use of TR commands in Linux systems tutorial

Source: Internet
Author: User

Usage: TR [options] ... SET1 [SET2]

Replaces, shrinks, and/or deletes characters from standard input and writes the results to standard output.

-C,-C,--complement first to complement SET1

-D,--delete deletes the contents of the matching SET1 and does not replace

-S,--squeeze-repeats if the character that matches the SET1 exists in the input sequence in successive

Repeat, and will be uniformly indented to the length of one character at the time of substitution

-T,--truncate-set1 first truncate the length of SET1 to equal to SET2

--HELP displays this help information and exits

--version display version information and exit

A set is a set of strings that can generally be understood in terms of literal meaning. The parsing sequence is as follows:

NNN octal value is NNN (1 to 3 digits)

Back slash

A terminal ringing

b BACKSPACE

F Page Change

N Line Wrapping

R carriage

T horizontal tab

V Vertical Tabs

Character 1-character 2 all characters experienced in ascending increments from character 1 to character 2

[Character *] is applied in SET2, the specified character is copied continuously until the length of the set 1 is matched

[Character * Count] performs a specified number of copies of a character, and is considered a octal number if the number starts with 0

[: Alnum:] All letters and numbers

[: Alpha:] all the letters

[: blank:] All horizontal white-space characters

[: Cntrl:] All control characters

[:d Igit:] All the numbers

[: Graph:] All printable characters, excluding spaces

[: Lower:] All lowercase letters

[:p rint:] All printable characters, including spaces

[:p UNCT:] All punctuation characters

[: space:] all horizontal or vertical whitespace characters

[: Upper:] All uppercase letters

[: xdigit:] All hexadecimal numbers

[= character =] All characters equal to the specified character

Substitutions are only made when both SET1 and SET2 are given and there is no-d option.

The-t option may be used only when replacing. If you need SET2 will be by adding the original last character at the end of the way

Added to the same length as SET1. The extra characters in the SET2 will be omitted. Only [: Lower:] and [: Upper:]

Expands the character in ascending order, and the case conversion is represented in pairs in the SET2 for substitution. -S acting on SET1, neither

Replacement is not deleted, otherwise use SET2 reduction after replacement or expansion.

The parameter-C in the TR command is more practical, you can replace the character set with the complement set of the character set in string 1, requiring the character set to be a S c i. Then try a command on the terminal:

The code is as follows:

Tr-cs "[A-z][a-z]" "[12*]"

This converts the Diary.txt data within the file to a newline character, and then deletes all occurrences of the sequence with the-s option, leaving only the first; the string is compressed into a string.

When I looked at this command carefully, I found a * on the back of 12, and when I removed the *, I ran, found the results all on the same line and separated them with the left bracket, and I tried to use n instead of 12,

The discovery is also only after the N plus * to arrive at the correct result. Later, the brackets are removed. This is not * when you can output the correct results, send and add the * will output the results in a row, the middle with * separated.

Guess: If you add the parentheses, the special characters inside are added *, whether they are expressed in letters or octal numbers.

Here are a few more examples:

The code is as follows:

[Root@localhost zhangy]# echo "TANK" |tr A-Z-z #大写字母转小写

Tank

The code is as follows:

[Root@localhost zhangy]# Echo ' Tank Zhang ' | TR-Z-z #小写字线转大写

TANK ZHANG

The code is as follows:

[Root@localhost zhangy]# cat Aaa.txt #原文件

Aaa

Bbb

The code is as follows:

[Root@localhost zhangy]# cat aaa.txt|tr ' A ' C ' #字母c替换字母a

Ccc

Bbb

The code is as follows:

[Root@localhost zhangy]# cat aaa.txt|tr-d ' a ' #删除所有字母a

Bbb

The code is as follows:

[Root@localhost zhangy]# cat aaa.txt|tr-d ' NT ' deletes the newline ' n ', tab ' t ' character appearing in file files

aaabbb

The code is as follows:

[Root@localhost zhangy]# cat aaa.txt|tr-s [a-za-z] #删除重复的字母

A

B

The code is as follows:

[Root@localhost zhangy]# cat aaa.txt|tr-s ' n ' #删除空行

Aaa

Bbb

The code is as follows:

[Root@localhost zhangy]# cat aaa.txt |tr-s ' #用空格符40替换制表符11

Aaa

Bbb

The code is as follows:

# TR A C < test #将test文件中的a变成c

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.