Use of the Linuxtr command

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... information & nbsp; 1. you can easily implement many basic functions of sed by using tr. 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]" in oops.txt Result.txt (2) delete empty lines 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 *]" Stat. tmp replace ^ M at the end of each line with a new line, and remove ^ Z with \ n. the input must come from the temporary working file stat. tmp. Tr-s "[\ 015] [\ 032]" "\ n" UNIX: tr "\ r" "\ n" Unixfile UNIX-> Mac: tr "\ n" "\ r" In 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" 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 "}' Dosfile author tianshuai11
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.