Use of TR commands

Source: Internet
Author: User
1. About tr
By using TR, you can easily implement many basic sed functions. You can view tr as SED
(Extremely) Simplified variant: 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
Delete all recurring character sequences and retain only the first one. Compress a recurring string into one.
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-Z]
A string consisting of characters in a-Z.
[A-Z] A string consisting of characters within the A-Z.
[0-9] numeric string.
\ Octal
A three-digit octal number, corresponding to valid ASCII characters.
[O * n]
The specified number of times that the character o appears again is N. Therefore, [O * 2] matches the OO string.
Different expressions of specific control characters in tr
Shorthand meaning octal
\ A Ctrl-G
Ringtone \ 007
\ B Ctrl-H return character \ 010
\ F Ctrl-l 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 repeated lower-case characters in oops.txt
Tr-s "[A-Z]" <oops.txt
> Result.txt
(2) Delete empty rows
Tr-s "[\ 012]" <plan.txt or tr-s ["\ n"] <
Plan.txt
(3) Delete the ^ m in the file and wrap it with a line feed.
Tr-s "[\ 015]" "[\ n]" <file or tr-S
"[\ R]" "[\ n]" <File
(4) uppercase to lowercase
Cat a.txt | TR "[A-Z]" "[A-Z]"
> B .txt
(5) delete a specified character

Calendar of one week. 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 need to specify the two character ranges [A-Z] and [A-Z], the command tr-CS
"[A-Z] [A-Z]" [\ 012 *]"
Place all strings in each line of the file that are not included in [A-Z] or [A-Z] (all Greek letters) in string 1 and convert them to a new line. The-s option indicates compressing all new rows,
-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) conversion control characters

The first function of 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 break conversion by carriage return. Cat-V filename
Display control characters.
Cat-V stat.txt
Box AA ^ 12 ^ m
Apple
Bbas ^ 23 ^ m
^ Z
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. ^ Octal Code Is 136, ^ m is 015, the tab key is 011, and ^ Z is 032
, The final function will be completed in the following steps.
Replace ^ With the tab key. The command is "\ 136"
"[\ 011 *]". Redirect results to temporary workers Composition Agent Stat. tmp
Tr-s "[\ 136]" [\ 011 *] "<stat.txt
> Stat. tmp
Replace ^ m at the end of each row 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" <Stat. tmp
To delete all the tab keys, replace them with spaces and use the command
Tr-s "[\ 011]"
"[\ 040 *]" <input. File

(7) replace all the colons in the passwd file with the tab key to increase readability
tr-s "[:] "" [\ 011] " or tr-s" [:] "" [\ t] " (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 together with colons.
the tr command can convert these colons into carriage return. In this way, these paths are quite readable.
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 a Mac OS or DoS/Windows
machine, you will find tr very useful.
If the file is not saved as a Unix line break to indicate the end of the line format, you need to convert the file to the Unix format.
, otherwise, the Program does not process these files correctly. The line at the end of Mac OS
ends with a carriage return character (\ r). Many text processing tools process such files as one line. To solve this problem, you can use the following tips:
Mac-> Unix: tr
"\ r" "\ n" unixfile
Unix-> Mac: TR "\ n" "\ r" MacFile
Microsoft DOS/Windows
indicates that each line of text is carriage return (\ r) end with the line break (\ n. To solve this problem, run the following command:
DOS-> Unix: tr-D
"\ r" unixfile
Unix->
DoS: in this case, awk, because tr cannot insert two characters to replace one character. The awk command to be used is awk '{print $0 "\ r"
}' dosfile
Note: Both can be done with sed. Note: From http://blogold.chinaunix.net/u/12367/showart_235875.html

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.