Although the contact with Linux for several years, but never used the TR command before learning to find that it is actually a very simple but very useful text processing tool.
First look at the man's description of it:
NAME tr - translate or delete charactersSYNOPSIS tr [OPTION]... SET1 [SET2]DESCRIPTION Translate, squeeze, and/or delete characters from standard input, writing to standard output.
You can also view specific information by command info coreutils ' tr invocation ' :
Synopsis: tr [OPTION]... SET1 [SET2] ‘tr‘ copies standard input to standard output, performing one of thefollowing operations: * translate, and optionally squeeze repeated characters in the result, * squeeze repeated characters, * delete characters, * delete characters, then squeeze repeated characters from the result.
You can see that, strictly speaking, TR accepts standard output processing and then passes it to standard output to print out, and it cannot directly process text files.
Let's take a look at the usage:
TR [OPTION] ... SET1 [SET2]
Where parameters:
-C,--complement: The inverse of the set character. Replace this character set with the complement of the character set SET1, which requires the character to be an ASCII code
-D,--delete: Delete instruction character
-S,--squeeze-repeats: reduces the continuous repetition of the word literal the specified single character
-T,--truncate-set1: Cuts the SET1 specified range so that it is equal to the SET2 set length
Character Range
When specifying the contents of a string SET1 or string SET2, only single character or string range or list can be used. The following is an acceptable string range and list:
A string that consists of characters in A-Z (A-Z).
[A-z] A string consisting of characters within a-Z.
[0-9] number string.
\octal a three-bit octal number that corresponds to a valid ASCII character.
[on] indicates that the character o repeats the specified number of times N. So [O2] matches the OO string.
Different expressions of specific control characters in TR
Shorthand notation meaning octal method
\ \ counter Slash
\a ctrl-g Ringtones
\b Ctrl-h Backspace
\f Ctrl-l go to the line to change pages
\ ctrl-j New Line
\ r Ctrl-m Enter
\ t ctrl-i Tab key
\v ctrl-x Horizontal Tab
CHAR1-CHAR2: The character range is specified from CHAR1 to CHAR2, and the specified range is based on the order of the ASCII code, only from small to large, not from large to small.
[char*]: This is the SET2 dedicated setting, the function is to repeat the specified characters to the same length as SET1
[Char*repeat]: This is also the SET2 dedicated settings, function is to repeat the specified characters to the set number of REPEAT (REPEAT of the Digital Mining 8 decimal system, starting with 0)
[: Alnum:]: Letters and numbers can be used instead of ' a-za-z0-9 '
[: Alpha:]: Letters can be used instead of ' a-za-z '
[: Cntrl:]: Control (nonprinting) character
[:d igit:]: Number, can be used instead of ' 0-9 '
[: Graph:]: Graphic character
[: Lower:]: lowercase letter, can be used instead of ' A-Z '
[:p rint:]: Printable characters
[:p UNCT:]: Punctuation
[: Space:]: white space character
[: Upper:]: Uppercase, can be used instead of ' A-Z '
[: Xdigit:]: hexadecimal character
Here are some examples of usage, some of which relate to other commands I have learned recently, purely for the purpose of reviewing and deepening impressions in order to avoid forgetting.
1. Convert the contents of the/etc/issue file to uppercase and save to the/tmp/issue.out file
[[email protected] 15:23:00 ~]#tr ‘[:lower:]‘ ‘[:upper:]‘ > /tmp/issue.out < /etc/issue[[email protected] 15:23:26 ~]#cat /tmp/issue.out \SKERNEL \R ON AN \M[[email protected] 15:23:34 ~]#cat /etc/issue\SKernel \r on an \m[[email protected] 15:23:43 ~]#
This can also be used [A-z] [a-z] to replace the two character set after TR
2. Convert the current system login user's information to uppercase and save to the/tmp/who.out file
[[email protected] 15:36:28 ~]#w | tr [a-z] [A-Z] | tee >&1 /tmp/who.out 15:36:40 UP 6:13, 1 USER, LOAD AVERAGE: 0.00, 0.01, 0.05USER TTY FROM [email protected] IDLE JCPU PCPU WHATROOT PTS/0 172.20.95.99 14:48 0.00S 0.74S 0.00S W[[email protected] 15:36:40 ~]#
3./root/the list of files, displaying them as a line, separated by a space between the filenames
[[email protected] 15:41:47 ~]#ls /root/ | tr ‘\n‘ ‘ ‘anaconda-ks.cfg Desktop Documents Downloads hosts initial-setup-ks.cfg LISTEN ls.log Music passwd.out Pictures profile_test Public rge1.txt tee Templates test2.txt test3.txt test8.txt test.txt tr07.txt Videos [[email protected] 15:42:24 ~]#
4. Calculate 1+2+3+. The sum of +99+100
[[email protected] 15:45:08 ~]#echo {1..100} | tr ‘ ‘ ‘+‘1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16+17+18+19+20+21+22+23+24+25+26+27+28+29+30+31+32+33+34+35+36+37+38+39+40+41+42+43+44+45+46+47+48+49+50+51+52+53+54+55+56+57+58+59+60+61+62+63+64+65+66+67+68+69+70+71+72+73+74+75+76+77+78+79+80+81+82+83+84+85+86+87+88+89+90+91+92+93+94+95+96+97+98+99+100[[email protected] 15:45:23 ~]#echo {1..100} | tr ‘ ‘ ‘+‘ | bc5050[[email protected] 15:45:29 ~]#[[email protected] 15:45:29 ~]#seq -s + 1 100 | bc5050[[email protected] 15:53:03 ~]#
It's a lot easier than a loop.
5. Handle the string "Xt.,l 1 jr#! $mn 2 c*/fe 3 uz 4", preserving only the numbers and spaces
[[email protected] 15:56:19 ~]#echo ‘xt.,l 1 jr#bcmn 2 c*/fe 3 uz 4‘ | tr -d ‘[:alpha:]‘,‘[:punct:]‘ 1 2 3 4[[email protected] 15:56:26 ~]#
Description, the-D option is used to separate multiple character sets with a comma-readable, and of course two character sets can be in the same quotation mark
6. Display the path variable in a separate row for each directory
[[email protected] 15:56:26 ~]#echo $PATH | tr -s : ‘\n‘/usr/local/sbin/usr/local/bin/usr/sbin/usr/bin/root/bin[[email protected] 15:58:41 ~]#
7. Display each word in the file/etc/centos-release (composed of letters) on a separate line with no blank lines
[[email protected] 15:58:41 ~]#cat /etc/centos-release | tr -sc ‘[:alpha:]‘ ‘\n‘CentOSLinuxreleaseCore[[email protected] 16:00:48 ~]#
The deeper Linux can realize "small, single-purpose programs; Connect programs to complete complex functions together"
Linux TR Command Usage experience