Linux TR Command detailed

Source: Internet
Author: User
Tags control characters

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 another character with one character, or it can completely remove some characters. You can also use it to remove duplicate characters. This is what all TR can do.
TR is used to convert characters from standard input through substitution or deletion operations. TR is primarily used to remove control characters from a file or to convert characters. To convert two strings when using tr: string 1 is used for queries, and string 2 is used to handle a variety of conversions. When TR executes, the character in string 1 is mapped to the character in String 2, and then the conversion operation begins.
The TR command format with the most common options is:
Tr-c-d-s ["String1_to_translate_from"] ["string2_to_translate_to"] < Input-file
Over here:
-C replaces this character set with a complement to the character set in string 1, which requires the character set to be ASCII.
-d deletes all input characters in string 1.
-S Delete all occurrences of a sequence of characters, leaving only the first one; the string compression is about to recur to a string.
Input-file is the conversion file name. Although you can enter in other formats, this format is most commonly used.

2. Character Range
When specifying the contents of string 1 or String 2, only single character or string range or list can be used.
A string that consists of characters in A-Z (A-Z).
[A-z] A string consisting 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 expressions of specific control characters in TR
Shorthand notation meaning octal method
\a ctrl-g Ringtones \007
\b Ctrl-h backspace \010
\f Ctrl-l Walk the line to change page \014
\ ctrl-j New Line \012
\ r Ctrl-m Enter \015
\ t ctrl-i Tab key \011
\v Ctrl-x \030

Instance:

, replace the "ABC" that appears in the file with "XYZ"

# Cat File | TR "ABC" "XYZ" > new_file "Note" Here, all the "a" letters appearing in file are replaced by "X" Letters, "B" letters with "Y" letters, and "C" letters replaced by "Z" letters. Instead of replacing the string "ABC" with the string "xyz". 2. Use the TR command "uniform" letter case (lowercase-to-uppercase)
# Cat File | tr [A-z] [a-z] > new_file (Uppercase-to-lowercase)
# Cat File | tr [A-z] [a-z] > New_file 3, replace the number 0-9 in the file with the A-j # cat File | TR [0-9] [a-j] > New_file
4. Delete "Snail" characters appearing in file files # cat File | tr-d "Snail" > new_file "Note" Here, the ' S ', ' n ', ' a ', ' I ', and ' l ' characters that appear in the file files will be deleted! Instead of tightly deleting the "Snail" string that appears. 5. Delete the newline ' \ n ' and the tab ' \ T ' characters that appear in file files # cat File | tr-d "\n\t" > New_file invisible characters are represented by an escape character, which is uniform. 6. Delete "consecutive" repeating letters and keep only the first # cat file | tr-s [A-za-z] > New_file 7, delete empty line # cat File | Tr-s "\ n" > New_file 8, delete windows file "Cause" the ' ^m ' character # cat file | Tr-d "\ r" > New_file
Or
# Cat File | Tr-s "\ r" "\ n" > new_file "Note" Here-S is followed by two parameters "\ r" and "\ n", replace the former 9 with the latter, with a space character \040 replace tabs \011 # cat File | Tr-s "\011" "\040" > New_file 10, the colon ":" In the path variable, replaced with the line break "\ n" # echo $PATH | Tr-s ":" \ n "

Linux TR Command detailed

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.