Basic usage of the TR command to delete and replace text characters in Linux systems

Source: Internet
Author: User
Tags character set control characters first string lowercase valid

By using TR, you can easily implement many of the most basic features of SED. You can see TR as a (extremely) simplified variant of sed: it can replace one character with another, or you can completely remove some characters. You can also use it to remove duplicate characters. That's all the TR can do.

TR is used to convert characters from standard input through substitution or deletion operations. TR is used primarily to remove control characters from files or to convert characters. To convert two strings when using tr: string 1 is used for queries, and string 2 is used to handle various transformations. When TR was first executed, the character in string 1 was mapped to the character in String 2, and the conversion operation began.

The TR command format with the most common options is:

The code is as follows:

Tr-c-d-s ["String1_to_translate_from"] ["string2_to_translate_to"] < Input-file

Over here:

-C replaces this character set with the complement set of the character set in string 1, requiring the character set to be ASCII.

-d deletes all input characters in string 1.

-S deletes all occurrences of a sequence of characters, leaving only the first one; the string is compressed into a string that repeats itself.

Input-file is the name of the transformation file. Although you can use other format inputs, this format is most commonly used.

Character Range

When you specify the contents of string 1 or String 2, you can use only single characters or string ranges or lists.

[A-z] a string consisting of characters within a-Z.

[A-z] A string that consists of characters within a-Z.

[0-9] number string.

Octal a three-bit octal number that corresponds to a valid ASCII character.

[O*n] indicates that the character O repeats the specified number of times N. therefore [o*2] matches the OO string.

Different ways of expressing specific control characters in TR

Shorthand character meaning octal method

A ctrl-g ringtones 07

b ctrl-h BACKSPACE 10

F ctrl-l Go to change page 14

N Ctrl-j New Line 12

R ctrl-m Carriage return 15

T ctrl-i Tab key 11

V Ctrl-x 30

A special sequence of expression strings

The strings contained in the STRING1 and String2 variables can be represented using the following conventions:

If a character is specified more than once in String1, the character is converted to the character story corresponding to the last occurrence of the String1 in String2.

If the string length specified by String1 and String2 is not the same, the TR command ignores the extra characters in a longer string.

Sign

Exit status

This command returns the following export values:

Instance:

1. Replace "abc" with "XYZ" appearing in file files

Copy Code

The code is as follows:

# Cat File | TR "ABC" "XYZ" > New_file

"Note" Here, all the "a" letters that appear in file are replaced with "X" Letters, "B" letters replace "Y" letters, "C" letters with "Z" letters. Instead of replacing the string "ABC" with the string "xyz".

2, use the TR command "unified" letter case

(Lowercase--> uppercase)

The code is as follows:

# Cat File | tr [A-z] [a-z] > New_file

(Uppercase--> lowercase)

The code is as follows:

# Cat File | tr [A-z] [a-z] > New_file

3. Replace the number 0-9 in the file with the A-j

The code is as follows:

# Cat File | TR [0-9] [a-j] > New_file

4, delete the "Snail" characters appearing in file files

The code is as follows:

# Cat File | tr-d "Snail" > New_file

"Note" Here, all the ' S ', ' n ', ' a ', ' I ', ' l ' characters that appear in the file files are deleted! Instead of tightly deleting the "Snail" string that appears.

5. Delete the "n" and Tab ' t ' characters appearing in file files

The code is as follows:

# Cat File | Tr-d "NT" > New_file

The invisible character is represented by an escape character, which is uniform.

6, delete "continuous" repeated letters, only the first

The code is as follows:

# Cat File | tr-s [a-za-z] > New_file

7, delete blank line

The code is as follows:

# Cat File | Tr-s "n" > New_file

8, delete windows file "caused" by the ' ^m ' character

The code is as follows:

# Cat File | Tr-d "R" > New_file

Or

The code is as follows:

# Cat File | Tr-s "R" "N" > New_file

"Note" Here is the two parameter "R" and "N", with the latter replacing the former

9, with spaces 40 replace the tab 11

The code is as follows:

# Cat File | Tr-s "One" "> New_file"

10, the path variable in the colon ":" to replace the line character "n"

The code is as follows:

# echo $PATH | Tr-s ":" "N"

11. To convert curly braces to parentheses, enter:

The code is as follows:

Tr ' {} ' () ' < textfile > NewFile

This converts each {(opening brace) to (left parenthesis) and converts each} (closing brace) to a right parenthesis. All other characters remain unchanged.

12. To convert curly braces to square brackets, enter:

The code is as follows:

Tr ' {} ' [] ' < textfile > NewFile

This converts each {(left curly brace) to [(opening parenthesis) and converts each} (closing brace) to] (right bracket). The opening parenthesis must be entered with a "" (backslash) escape character.

13. To convert lowercase characters to uppercase, enter:

The code is as follows:

Tr ' A-Z ' A-Z ' < textfile > NewFile

14. To create a list of words in a file, please enter:

The code is as follows:

Tr-cs ' [: lower:][:upper:] ' [n] ' < textfile > NewFile

This converts the characters of each sequence (except large and lowercase letters) to a single line break. * (asterisk) enables the TR command to repeat a newline character enough times to make the second string as long as the first string.

15. To remove all the null characters from a file, enter:

The code is as follows:

Tr-d ' ' < textfile > NewFile

16. To replace one or more lines of each sequence with a separate newline, enter:

The code is as follows:

Tr-s ' n ' < textfile > NewFile

Or

The code is as follows:

Tr-s ' ' < textfile > NewFile

17. To "?" (question mark) to replace each nonprinting character (except for valid control characters), enter:

The code is as follows:

Tr-c ' [:p Rint:][:cntrl:] ' [? *] ' < textfile > NewFile

This scans files created in different locales to find characters that cannot be printed in the current language environment.

18. To replace each character sequence in a character class with a single "#" character, enter:

The code is as follows:

Tr-s ' [: Space:] ' [#*] '

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.