Linux tr command: replace, abbreviate and/or delete characters from standard input
Linux tr command Function Description
Use the tr command to replace, reduce and/or delete characters from standard input and write the result to standard output. Replacement is performed only when both string 1 and string 2 are specified and there is no -d option.
The tr command can replace, compress, and delete characters from standard input. It can turn one set of characters into another, and is often used to write beautiful one-line commands that are very powerful.
Linux tr command Syntax
tr [Option] [String 1] [String 2]
The meaning of each option in the command is shown in the following table.
Option | Description |
---|---|
-c or –complerment | First complete the string 1 |
-d or –delete | Delete the content of match string 1, and do not replace it |
-s or –squeeze-repeats | If there are consecutive repetitions of characters matching string 1 in the input sequence, they are uniformly reduced to the length of one character when replaced |
-t or –truncate-set1 | First truncate the length of string 1 to be equal to that of string 2 |
Strings can generally be understood literally. The parsing sequence shown in the following table can be used in the tr command.
- String 1: Specifies the original character set to be converted or deleted. When performing the conversion operation, you must use the parameter String 2 to specify the target character set for conversion. However, when performing a delete operation, the parameter String 2 is not required.
- String 2: Specifies the target character set to be converted.
Parsing Sequence | Description |
---|---|
\NNN |
Octal value of NNN characters (1 to 3 digits) |
\\ |
Backslash |
\a |
Terminal chirp |
\b |
Backspace |
\f |
Change page |
\n |
A newline |
\r |
enter |
\t |
Horizontal TAB characters |
\v |
Vertical TAB characters |
Character 1- Character 2 |
All characters experienced in ascending order from Character 1 to Character 2 |
character * |
For Character 2, the specified characters will be copied continuously until they match the length of Character 1. |
Characters * times |
The character is copied a specified number of times, and if the number starts with 0, it is considered as an octal number. |
[:alnum:] |
All letters and numbers |
[:alpha:] |
All letters |
[:blank:] |
All horizontally aligned blank characters |
[:cntrl:] |
All control characters |
[:digit:] |
All numbers |
[:graph:] |
All printable characters, excluding spaces |
[:lower:] |
All lowercase letters |
[:print:] |
All printable characters, including spaces |
[:punct:] |
All punctuation characters |
[:space:] |
All blank characters in horizontal or vertical alignment |
[:upper:] |
All capital letters |
[:xdigit:] |
All hexadecimal numbers |
[=characters=] |
All characters equal to the specified character |
Linux tr command Example
Display the /root/apidemos.com.txt file and replace lowercase letters with uppercase letters in the output
Display the contents of the /root/a file, replacing the character a with d in the output
[root@rhel ~]# cat /root/a|tr a d
d
A
b
B
Converts input characters from uppercase to lowercase
echo "HELLO WORLD" | tr 'A-Z' 'a-z'
hello world
‘A-Z’ and ‘a-z’ are sets, and sets can be formulated by themselves, for example: ‘ABD-}’, ‘bB.,’, ‘a-de-h’, ‘a-c0-9’ are all sets, and ‘\n’, ‘\t’ can be used in sets, and other ASCII characters can be used.
Use tr to delete characters
echo "hello 123 world 456" | tr -d '0-9'
hello world
Converting tabs to spaces
cat text | tr '\t' ' '
Character set complement, remove all characters from the input text that are not in the complement
echo aa.,a 1 b#$bb 2 c*/cc 3 ddd 4 | tr -d -c '0-9 \n'
1 2 3 4
In this example, the complement set contains the numbers 0~9, spaces and the line break \n, so they are not deleted, and all other characters are deleted.
Compress characters with tr to compress repetitive characters in the input
echo "thissss is a text linnnnnnne." | tr -s ' sn'
this is a text line.
Clever use of tr for number summation
echo 1 2 3 4 5 6 7 8 9 | xargs -n1 | echo [(tr '\n' '+') 0 ]
Remove the ‘^M’ character from the Windows file "caused by"
cat file | tr -s "\r" "\n" > new_file
or
cat file | tr -d "\r" > new_file
The character classes that tr can use
[:alnum:]: letters and numbers
[:alpha:]: letters
[:cntrl:]: control (non-printing) characters
[:digit:]: numbers
[:graph:]: graph characters
[:lower:]: lowercase letters
[:print:]: printable characters
[:punct:]: punctuation characters
[:space:]: blank characters
[:upper:]: uppercase letters
[:xdigit:]: hexadecimal characters
Usage:
tr '[:lower:]' '[:upper:]'