Linux TR Command detailed

Source: Internet
Author: User
Tags control characters

Linux TR Command detailed

     by using TR, you can easily implement many of the most basic functions 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
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 Delete all input characters in string 1.
-s Delete all occurrences of the sequence of characters, leaving only the first one, and the string being compressed 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 string that consists of characters in A-Z (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//or TR "abc" "XYZ" < filename  "Note" Here, all the "a" letters that appear 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, using 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 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 ', tab ' \ t ' characters that appear in file # cat File | tr-d "\n\t" > New_file Invisible characters are represented by an escape character, which is uniform.  6. Delete "consecutive" repeating letters, leaving only the first one # Cat File | tr-s [a-za-z] > New_file 7. Delete empty lines # cat File | tr-s "\ n" > New_file 8. Delete Windows file "caused" by ' ^m ' character # cat File | tr-d "\ r" > New_file
or
# cat File | tr-s "\ r" "\ n" > New_file "Note" here the-S is followed by two parameters "\ r" and "\ n", replacing the former with the latter. The order is replaced first, then the weight is  9. Replace tab with character \040 \011 # cat File | tr-s "\011" "\040" > New_file 10. Replace the colon ":" in the path variable with the line break "\ n" # echo $PATH | tr-s ":" \ n "

Linux TR Command detailed

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.