The tr command is designed to change individual characters to different characters or to delete or reduce the number of characters.
Syntax: cat file(s) | tr '1st_char_group' '2nd_char_group'
- Displays the content of the files.
- Each character in '1st_char_group' being changed to the corresponding character in '2nd_char_group2'.
The tr command cannot take a file name as an argument.
The tr command can handle ranges of characters:
- Example: cat file1 | tr '[a-z]' '[A-Z]'
- This will display the contents of file1 with all letters upper-cased.
Ideally there are the same number of characters in each group.
- Example: cat file1 | tr 'abc' '123'
- This would display the contents of file1 with:
- Every 'a' changed to a '1',
- Every 'b' changed to a '2' and
- Every 'c' changed to a '3'.
tr Options
- -s Reduces sequences of a character to a single occurrence.
- -d Deletes every occurrence of a character.
- -c Acts only on the characters NOT listed. With -d, deletes characters not listed.
The tr command is case sensitive and works on individual characters, not whole words.
NOTE:
- Do not return data directly to the source file. You may end up with an empty file.
- The tr command is not effected by the LC_COLLATE setting.