Introduction to the use of tr command
tr
translate or delete characters
I understand this to mean converting characters or deleting unwanted characters
tr [OPTION]... SET1 [SET2]
Convert characters from standard input from SET1 to SET2
Example: Convert all upper case to lower case
#echo "HELLO HOW ARE YOU? " | tr 'A-Z' 'a-z'
Note: This can only be used for stdin.
Common options:
-c: Complement set, the so-called complement set is all the characters except the given character. This option cannot be used alone.
-d: Delete specified character
-s: compresses specified duplicate characters
Examples of usage:
-d: For example, you can see that the specified collection has been deleted
#echo "Hello 1 how 2 are 3 you 4 ? " |tr -d '[0-9]' (brackets can be omitted)
-c: As you can see from the example, the specified set is all numbers, spaces, newlines, and because of the-d option, everything except the set is deleted.
#echo "Hello 1 how 2 are 3 you 4 ? " | tr -d -c '0-9 \n'
-s: As you can see from the example, the redundant spaces in the middle are compressed and only one is displayed
# echo "Hello 1 how 2 are 3 you 4 ? " | tr -s ' '
Definition set:
'A-Z' and 'a-z' are both sets, we can append characters or character classes as needed to construct sets
'Start character-end character': the way in which the meaning of a continuous set must be continuous, if not continuous, it is defined as 3 single elements, i.e. the start character,'-','end character';
Special characters: '\t' for tabs,'\n' for newlines
Character class:
alnum: all letters and numbers
digit: all numbers
alpha: all letters
space: white space character
upper: all upper case characters
lower: all lowercase characters
punct: punctuation
xdigit: hexadecimal character
How to use:
tr [: Character class:] [: Character class:]