By using TR, you can easily implement many of the most basic features of SED. You can see TR as a (extremely) simplified variant of sed: it can replace one character with another, or you can completely remove some characters. You can also use it to remove duplicate characters. That's all the TR can do.
TR is used to convert characters from standard input through substitution or deletion operations. TR is used primarily to remove control characters from files or to convert characters. To convert two strings when using tr: string 1 is used for queries, and string 2 is used to handle various transformations. When TR was first executed, the character in string 1 was mapped to the character in String 2, and the conversion operation began.
The TR command format with the most common options is:
Tr-c-d-s ["String1_to_translate_from"] ["string2_to_translate_to"] < Input-file
Over here:
-C replaces this character set with the complement set of the character set in string 1, requiring the character set to be ASCII.
-d deletes all input characters in string 1.
-S deletes all occurrences of a sequence of characters, leaving only the first one; the string is compressed into a string that repeats itself.
Input-file is the name of the transformation file. Although you can use other format inputs, this format is most commonly used.
1. Character Range
When you specify the contents of string 1 or String 2, you can use only single characters or string ranges or lists.
[A-z] a string consisting of characters within a-Z.
[A-z] A string that consists of characters within a-Z.
[0-9] number string.
\octal a three-bit octal number that corresponds to a valid ASCII character.
[O*n] indicates that the character O repeats the specified number of times N. therefore [o*2] matches the OO string.
Different ways of expressing specific control characters in TR
Shorthand character meaning octal method
\a ctrl-g Ringtones \007
\b Ctrl-h backspace \010
\f Ctrl-l Walk the line to change pages \014
\ ctrl-j New Line \012
\ r ctrl-m Carriage return \015
\ ctrl-i Tab key \011
\v Ctrl-x \030
Instance:
, replace "abc" with "XYZ" # Cat File | TR "ABC" "XYZ" > new_file "Note" Here, all "a" letters that appear in file are replaced with "X" Letters, "B" letters replace "Y" letters, "C" letters with "Z" letters. Instead of replacing the string "ABC" with the string "xyz".2, use the TR command "unified" letter case
(Lowercase--> uppercase)
# Cat File | tr [A-z] [a-z] > new_file (uppercase--> lowercase)
# Cat File | tr [A-z] [a-z] > New_file
3. Replace the number 0-9 in the file with the A-j
# Cat File | TR [0-9] [a-j] > New_file
4, delete the "Snail" characters appearing in file files
# Cat File | tr-d "Snail" > new_file "Note" Here, all the ' S ', ' n ', ' a ', ' I ', ' l ' characters that appear in the file files are deleted. Instead of tightly deleting the "Snail" string that appears.
5. Delete the newline ' \ n ', tab ' t ' character appearing in file files
# Cat File | tr-d "\n\t" > New_file invisible characters are represented by escape characters, which are all unified.
6, delete "continuous" repeated letters, only the first
# Cat File | tr-s [a-za-z] > New_file
7, delete blank line
# Cat File | Tr-s "\ \" > New_file
8, delete windows file "caused" by the ' ^m ' character
# Cat File | Tr-d "\ r" > New_file
Or
# Cat File | Tr-s "\ r" "\ n" > new_file "Note" Here-S is two arguments "\ r" and "\ n", replacing the former with the latter
9. Replace tabs with spaces \040 \011
# Cat File | Tr-s "\011" "\040" > New_file
10, the path variable in the colon ":" to replace the line character "\ n"
# echo $PATH | Tr-s ":" \ n "
This article source: http://blog.csdn.net/trochiluses/article/details/10110551