Description of tr commands in Linux

Source: Internet
Author: User

The Linux operating system has received a lot of attention. The Linux operating system has caught up with Microsoft, so many people learn about the Linux operating system. tr is a very practical tool for the Linux operating system, in fact, almost all things done with tr can be done with sed. You can regard tr as the sed "simplified" that implements many basic functions of sed ". Tr is considered as a simple "character" processing tool, while sed is a very powerful "string" processing tool.

Note: tr is a single character processing tool, not a string processing tool!

I have used the following useful examples:

1. Replace "abc" in the file with "xyz"

# Cat file | tr "abc" "xyz"> new_file

Note: replace all "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" uppercase and lowercase letters
Lower case --> upper case)
# Cat file | tr [a-z] [A-Z]> new_file
Upper case --> lower case)
# 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_file

4. 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 that appear 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, the-s is followed by two parameters "\ r" and "\ n". Replace 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"

This shows whether the PATH variables are clearer. Through this article, you will understand the tr commands in the Linux operating system.

  1. Easily connect to the campus network using Linux
  2. Fully Understand Linux Process Technology
  3. Introduction to the password command in Linux
  4. Learn about Linux regulatory commands
  5. How to view the ps-aux command of all processes in Linux

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.