This chapter is from the book
tr
The tr command is useful for translating characters from one set to another. The syntax of the command is tr SET1 [SET2].
For example, the following will capitalize the output of the date command:
[student@localhost dictionary]$ date Sat Dec 3 20:15:05 PST 2016 [student@localhost dictionary]$ date | tr 'a-z' 'A-Z' SAT DEC 3 20:15:18 PST 2016
Note that in order to use the tr command on a file, you must redirect the file into the tr command, like so, because the tr command does not accept files as arguments:
tr 'a-z' 'A-Z' < file
Important options include the following:
Option | Description |
---|---|
-d | Used when the second set is omitted; it deletes the matching characters. For example, the following deletes all numbers from the output of the date command: date | tr -d '0-9'. |
-s | Repeated matching characters are converted into a single character before being translated. Thus, “aaabc” would be converted into “abc” and then translated to “Abc” if the command tr -s 'a' 'A' were executed. |