Linux tr commands

Source: Internet
Author: User
Tags control characters first string

By using TR, you can easily implement many basic sed functions. You can use tr as the (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 replacement or deletion. TR is mainly used to delete control characters in a file or convert characters. When using TR, You need to convert two strings: String 1 for query and string 2 for various conversions. When Tr is just executed, the characters in string 1 are mapped to the characters in string 2, and the conversion operation starts.
    The format of the tr command with the most common options is:
Tr-c-d-s ["string1_to_translate_from"] ["string2_to_translate_to"] <input-File
Here:
-C: replace this character set with the supplement set of Character Set 1. the character set must be ASCII.
-D: delete all input characters in string 1.
-S deletes all recurring character sequences and retains only the first string. The duplicate strings are compressed into one string.
Input-file is the conversion file name. Although other formats can be used for input, this format is the most commonly used.

2. character range
When specifying the content of string 1 or string 2, you can only use a single character or string range or list.
A string consisting of characters in a-Z.
[A-Z] A string consisting of characters within the A-Z.
[0-9] numeric string.
\ Octal is a three-digit octal number, which corresponds to valid ASCII characters.
[O * n] indicates the repeated occurrence of the character O for a specified number of times. Therefore, [O * 2] matches the OO string.
Different expressions of specific control characters in tr
Shorthand meaning octal
\ A Ctrl-GRingtone \ 007
\ B Ctrl-HEscape Character \ 010
\ F Ctrl-lLine feed \ 014
\ N Ctrl-JNew Line \ 012
\ R Ctrl-mPress enter \ 015
\ T Ctrl-ITab key \ 011
\ V Ctrl-x\ 030

Instance:

, Replace "ABC" in the file with "XYZ"

  # Cat file | TR "ABC" "XYZ"> new_file Note: replace all the "A" letters in the file with "X" letters, and "B" with "Y" letters, replace the "c" letter with the "Z" letter. Instead of replacing the string "ABC" with the string "XYZ ". 2. Use the tr command to "unify" the case (lower case --> upper case)
# Cat file | tr [A-Z] [A-Z]> new_file (uppercase --> lowercase)
# Cat file | tr [the A-Z] [A-Z]> new_file 3. Replace 0-9 with a-J. # Cat file | tr [0-9] [A-J]> new_file4. Delete the "Snail Il" character in the file. # Cat file | tr-d "Snail Il"> new_file Note: All the 's', 'n', 'A', 'I', and 'l' characters in the file are deleted! Instead of deleting the "Snail Il" string. 5. Delete the newline '\ n' and tabulation' \ t' characters in the file. # Cat file | tr-d "\ n \ t"> new_file Invisible characters must be expressed by escape characters, which are unified. 6. Delete "consecutive" Duplicate letters and retain only the first one # Cat file | tr-s [A-Za-Z]> new_file 7. Delete empty rows # Cat file | tr-s "\ n"> new_file 8. Delete the '^ M' character "caused" by the Windows File # 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", replacing the former with the latter 9. Use the space character \ 040 to replace the Tab character \ 011 # Cat file | tr-s "\ 011" "\ 040"> new_file 10. Replace the colon ":" In the PATH variable with the linefeed "\ n"

# Echo $ PATH | tr-s ":" "\ n"

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.