Linux: tr command usage

Source: Internet
Author: User
For linux: tr command usage, tr can be viewed as a simplified sed tool. tr is represented as: translate. The tr command is mainly used to convert strings in the following two replacement operations. The string conversion of the delete operation can easily delete some control characters or empty rows. The tr command can be used in linux: tr to view tr as a simplified sed tool. tr is represented as: translate. The tr command is mainly used to convert strings in the following two replacement operations. The string conversion of the delete operation can easily delete some control characters or empty rows. All functions implemented by the tr command can be implemented by the sed command. However, for specific replacement functions, tr is easier to use and easier to use. 1. the command format is [html] tr [option] ["string1"] ["string2"] <file. common options include: default options. If there is no option, tr is replaced by the replacement operation by default, that is, replace the character that string1 appears in the file with the character in string2. here, note the replacement relationship. -C option, replace string1 with the character set in string1, where the character set is ASCII. -D option to delete all characters in string1. -S option to delete the repeated characters in the file that appear in string1. only one character is retained. -When using the c option, just replace string1 with the current complement set. for example, when using the C code [html] [root @ localhost client] # echo "hello world, root, 2012 "| tr-c" 0-9 "" * "***************** 2012, we use 0-9. after adding the-c option, we will replace 0-9 with its complementary set. in this case, the complementary set naturally does not contain 0-9, but contains many other characters, next, we will replace all other characters with the "*" character, but it does not contain numbers. C code [html] [root @ localhost client] # echo "hello world, root, 2012 "| tr" 0-9 "" * "hello world, root, *** 2. when the value range of a string is string or string2, 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. 3, different expressions of control characters shorthand character 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 pay attention to these control characters, if you want to enter ^ M in linux, press ctrl + V + M at the same time. 4. replacement of characters is the default operation of tr, first look at the following command and output C code [html] [root @ localhost client] # echo "hello world" | tr "a-z" "A-Z" hello world [root @ localhost client] # echo "hello world" | tr "a-l" "A-Z" HELLo worLD [root @ localhost client] # echo "hello world" | tr "a-z" "A-H "The first line of hehhh hhhhd output is to replace the lower case with the upper case. The second line of output will replace a-l in lower case with the A-L, and do not replace the characters after l in lower case. The output of the third line will replace a-h in lower case with a A-H, and all characters after h are replaced with H, because the replacement space of the latter is not as large as the character space before, so I will repeat the subsequent H, it is equivalent to a A-HHH......HHHHH. If you want to perform case-insensitive conversion, you can press the following input: C code [html] tr "a-z" "A-Z" <inputfile 5, remove duplicate characters this time, the-s option is used, for example, C code [html] [root @ localhost client] # echo "hello world, root" | tr-s "ao" hello world, rot [root @ localhost client] # echo "hello world, root" | tr-s "lo" helo world, rot [root @ localhost client] # echo "hello world, root "| tr-s" a-z "helo world, rot [root @ localhost client] # echo" hello world, root "| tr-s" 0-9 "hello w Orld. The first line of root indicates that only one duplicate character is removed from the input string in the "ao" character set. Because "hello world, root", only o meets the condition, the root is changed to rot, and the two o in the middle is changed to one. The second row compresses the hello and root characters. The third line removes all the distinct characters in a-z. The third line removes the repeated characters in the string from the 0-9 character set. If you want to remove empty rows, perform the following operations: [html] tr-s "\ n" <inputfile or tr-s "\ 012"

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.