- Linux Shell Scripting Cookbook(Third Edition)
- Clif Flynt Sarath Lakshman Shantanu Tushar
- 169字
- 2021-07-09 19:46:17
How it works...
Using tr with the concept of sets, we can map characters from one set to another set easily. Let's go through an example on using tr to encrypt and decrypt numeric characters:
$ echo 12345 | tr '0-9' '9876543210' 87654 #Encrypted $ echo 87654 | tr '9876543210' '0-9' 12345 #Decrypted
The tr command can be used to encrypt text. ROT13 is a well-known encryption algorithm. In the ROT13 scheme, characters are shifted by 13 positions, thus the same function can encrypt and decrypt text:
$ echo "tr came, tr saw, tr conquered." | tr 'a-zA-Z' 'n-za-mN-ZA-M'
The output will be the following:
ge pnzr, ge fnj, ge pbadhrerq.
By sending the encrypted text again to the same ROT13 function, we get this:
$ echo ge pnzr, ge fnj, ge pbadhrerq. | tr 'a-zA-Z' 'n-za-mN-ZA-M'
The output will be the following:
tr came, tr saw, tr conquered.
The tr can convert each tab character to a single space, as follows:
$ tr '\t' ' ' < file.txt