The use of powerful text manipulation command TR in Linux systems

Source: Internet
Author: User
Tags character set control characters lowercase printable characters readable

TR conversion, compression, truncation, deletion of characters

tr [OPTION].....SET1 [Set2]

Parameters:

-D,--Delete

Deletes the character in string 1 without converting.

-S,--squeeze-repeats

Compresses the recurring string into a string.

-T,--Truncate-set1

Truncate the length of string 1 to the length of string 2.

-C,--complement

Takes the complement of string 1. The character set is required to be ASCII.

--help print Help.

--version Display Moderator information.

The escape characters are as follows:

Octal code for NNN characters

Back slash

A Bell Bell

B Space

C does not print new line characters

N New Line

R carriage

T horizontal jump lattice

V Resolute to the jump lattice

Character mode:

Char1-char2 from character CHAR1 to CHAR2. such as a-z,a-z ...

[char*] represents one or several consecutive chars.

[Char*repeat] Represents a REPEAT of several chars.

[: Alnum:] represents all letters and numbers.

[: Alpha:] represents all letters.

[: Blank:] represents a horizontal space.

[: Cntrl:] Represents a control character.

[:d Igit:] represents all numbers.

[: Graph:] represents printable characters, excluding spaces.

[: Lower:] represents lowercase letters.

[: Upper:] represents uppercase letters.

[:p rint:] represents printable characters, including spaces.

[: Xdigit:] Represents a hexadecimal number.

[=char=] indicates CHAR

Example:

(1) Remove the repeated lowercase characters inside the Oops.txt

The code is as follows:

Tr-s "[A-z]" result.txt

(2) Delete blank line

The code is as follows:

Tr-s "[[]" < Plan.txt or tr-s ["n"] < Plan.txt

(3) Sometimes need to delete the ^m in the file, and replace the line

The code is as follows:

Tr-s "[]" [n] "< file or tr-s" [R] "[N]" < file

(4) Uppercase to lowercase

The code is as follows:

Cat A.txt |tr "[A-Z]" "[A-z]" >b.txt

(5) Delete the specified character

One-week schedule. The task is to remove all numbers from it and keep only the dates. The date has an uppercase and lowercase format. So you specify a two-character range [A-z] and [A-z], and the command Tr-cs "[A-z][a-z]" [12*] "places all the strings that are not contained in a [a-z] or [A-z] (all Greek letters) in string 1 and converts to a new row. The-S option indicates that all new rows are compressed, and-C indicates that all letters remain fixed. The original file is as follows, followed by the TR command:

The code is as follows:

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

(6) Conversion control characters

The first function of TR is to convert the control characters, especially when downloading files from Dos to UNIX, even when you forget to set FTP options for carriage return-line conversion. Cat-v filename Displays control characters.

The code is as follows:

Cat-v Stat.txt

Box aa^ ^^ ^^ 12^m

Apple bbas^ ^^ ^23^m

^z

The conjecture "^ ^ ^ ^ ^" is the TAB key. Each line ends with Ctrl-m, the end of the file Ctrl-z, and the following is the change method.

Use the-S option to view the ASCII table. ^ 's octal code is 136,^m is the 015,tab key is 011,^z is 032, the following steps to complete the final function.

With the TAB key Replace ^ ^ ^ ^ ^ ^, the command is "136" [11*]. REDIRECT results to temporary working file stat.tmp

The code is as follows:

Tr-s "[136]" "[11*]" stat.tmp

Replaces the ^m at the end of each line with the new row and ^z with N, and enters the temporary working file stat.tmp.

The code is as follows:

Tr-s "[15][32]" "N"

To remove All tab keys, replace them with spaces, and use the command

The code is as follows:

Tr-s "[11]" "[40*]"

(7) Replace all colons in the passwd file, and replace the TAB key to increase readability

The code is as follows:

Tr-s "[:]" "[One]" </etc/passwd or tr-s "[:]" "[t]" </etc/passwd

(8) Make the path readable

If you use a similar command like echo $PATH or echo $LD _library_path to display the path information, we will see a bunch of paths that are connected with colons, and the TR command can convert these colons to carriage returns, so that these paths are very readable.

The code is as follows:

echo $PATH | TR ":" "N"

(9) All these commands can be used within VI! Just remember: Add the line range and exclamation point (!) you want to handle before the TR command, such as 1,$!tr-d ' t ' (dollar sign for last line).

Related Article

Beyond APAC's No.1 Cloud

19.6% IaaS Market Share in Asia Pacific - Gartner IT Service report, 2018

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.