Tr is short for translate. With this word, you can probably guess what it is! Yes, it can replace another string with one character string, or it can completely remove some characters. You can also use it to remove duplicate characters.
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. Replace string 1 with string 2. String 1 can be a regular expression. If String 2 is null or not listed, it is a delete operation. It is easy to understand that replacing string 1 with a null string is equivalent to deleting string 1!
In addition, all tr functions can be completed using sed. You can view tr as the (extremely) Simplified variant of sed.
The format of the tr command with the most common options is:
Tr-c-d-s ["string1_to_translate_from"] ["string2_to_translate_to"]
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 deletes all recurring character sequences and retains only the first string. The duplicate strings are compressed into one string.
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 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
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 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) 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 displays control characters.
Cat-v stat.txt
Box aa ^ 12 ^ M
Apple bbas ^ 23 ^ M
^ Z
Suppose '^' 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 *]". Redirect the result to the temporary working file 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]" </etc/passwd or tr-s "[:]" "[\ t]" </etc/passwd
(8) make the path readable
If similar commands such as echo $ PATH or echo $ LD_LIBRARY_PATH 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, which makes these paths 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 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 correct this problem, use the following tips:
Mac-> UNIX: tr "\ r" "\ n" <macfile> unixfile
UNIX-> Mac: tr "\ n" "\ r" <unixfile> macfile
In Microsoft DOS/Windows conventions, each line of text ends with a carriage return character (\ r) followed by a line break (\ n. To correct 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
From the column andy572633