Use of Linux tr commands

Source: Internet
Author: User

1. using tr, you can easily implement many basic functions of sed. 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 tr command with the most common options is in the format of tr-c-d-s ["string1_to_translate_from"] ["string2_to_translate_to"] <input-file> result 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. Www.2cto.com-s deletes all recurring character sequences and only keeps the first string. The recurring string is 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. The result is the name of the output file after conversion. You can also use the pipeline as the input, for example: cat a.txt | tr "[a-z]" "[A-Z]"> B .txt [note] found that only write a-z Replace "[a-z]" also works
2. When the character range specifies the content of string 1 or string 2, only single character or string range or list can be used. 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 stenographer meaning octal character \ a Ctrl-G ringtone \ 007 \ B Ctrl-H Escape Character \ 010 \ f Ctrl-L go to line feed \ 014 \ n Ctrl-J New Line \ 012 \ r Ctrl-M press enter \ 015 \ t Ctrl-I tab key \ 011 \ v Ctrl-X \ 030 3, application Example (1 remove the repeated lowercase character tr-s "[a-z]" <oops.txt> result.txt (2) in oops.txt) delete the empty line tr-s "[\ 012]" <plan.txt or tr-s ["\ n"] <plan.txt (3) sometimes you need to delete ^ M in the file, newline tr-s "[\ 015]" "[\ n]" <file or tr-s "[\ r]" "[\ n]" <file (4) uppercase to lowercase www.2cto.com cat a.txt | tr "[a-z]" "[A-Z]"> B .txt
(5) Delete the calendar of a week with the specified character. The task is to delete all numbers from it and only retain the date. The date can be in uppercase or lowercase format. Therefore, you must specify two character ranges: [a-z] and [A-Z]. command tr-cs "[a-z] [A-Z]" "[\ 012 *]" to include all lines of the file not in [a-z] or [A-Z] (all Greece letter) put the string in string 1 and convert it to a new line. -S indicates that all new lines are compressed, and-c indicates that all letters are retained. The original file is as follows, followed by the tr command: tr-cs "[a-z] [A-Z]" "[\ 012 *]" <diary.txt (6) the first function of conversion control character tr is to convert control characters, especially when downloading files from dos to UNIX, especially when you forget to set the ftp option for line feed conversion by carriage return. Cat-v filename displays control characters. Cat-v stat.txt is displayed as: box aa ^ 12 ^ M apple bbas ^ 23 ^ M conjecture '^' is the tab key. Each line ends with Ctrl-M and the end of the file is Ctrl-Z. The following is the modification method. Use the-s option to view the ASCII table. ^ The octal code of ^ is 136, ^ M is 015, the tab key is 011, and ^ Z is 032. The final function will be completed as follows. Replace ^ With the tab key. The command is "\ 136" "[\ 011 *]". Redirects the result to the temporary working file stat. tmp tr-s "[\ 136]" [\ 011 *] "<stat.txt> stat. tmp replaces ^ M at the end of each row with a new line, and removes ^ Z with \ n. The input must come from the temporary working file stat. tmp. Tr-s "[\ 015] [\ 032]" "\ n" <stat. tmp: delete all the tab keys with spaces. Run the tr-s Command [\ 011] "[\ 040 *]" <input. file (7) replace all the colons in the passwd file with the tab key to increase readability of tr-s "[:] "" [\ 011] "</etc/passwd or tr-s" [:] "" [\ t] "</etc/passwd (8) make the PATH readable. If echo $ PATH or echo $ LD_LIBRARY_PATH and other similar commands are used to display the PATH information, we will see a lot of paths connected with colons, the tr command can convert these colons into carriage return, so that these paths are very readable. www.2cto.com echo $ PATH | tr ":" "\ n" (9) all these commands can be used in vi! Remember to add the line range and exclamation point (!) You want to process before the tr command (!), For example, 1, $! Tr-d '\ t' (dollar sign indicates the last line ). (10) In addition, when someone sends you a text file created on Mac OS or DOS/Windows, you will find tr very useful. If you do not save the file as a UNIX line break to indicate the end of the line format, you need to convert the file to the UNIX format. Otherwise, some command utilities will not process the files correctly. Mac OS ends with a carriage return character (\ r) at the end of the line. Many text processing tools process such files as one line. To solve this problem, use the following tips: Mac-> UNIX: tr "\ r" "\ n" <macfile> unixfile UNIX-> Mac: tr "\ n" "\ r" <unixfile> macfile Microsoft DOS/Windows conventions, each line of text ends with a carriage return character (\ r) followed by a line break (\ n. To solve this problem, run the following command: DOS-> UNIX: tr-d "\ r" <dosfile> unixfile UNIX-> DOS: in this case, awk is required, because tr cannot insert two characters to replace one character. The awk command to be used is awk '{print $0 "\ r"}' <unixfile> dosfile author tianshuai11

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.