tr
tr [OPTION]... SET1 [SET2]
Translate or delete characters
Options
--complement
,-c
,-C
-
use the complement of SET1
--delete
,-d
-
delete characters in SET1, do not translate
--squeeze-repeats
,-s
-
replace each sequence of a repeated character that is listed in the last specified SET, with a single occurrence of that character
--truncate-set1
,-t
-
first truncate SET1 to length of SET2
Translate, squeeze, and/or delete characters from standard input, writing to standard output.
Examples
Replace all occurrences of a character in a file, and print the result:
tr {{find_character}} {{replace_character}} < {{path/to/file}}
Replace all occurrences of a character from another command's output:
echo {{text}} | tr {{find_character}} {{replace_character}}
Map each character of the first set to the corresponding character of the second set:
tr '{{abcd}}' '{{jkmn}}' < {{path/to/file}}
Delete all occurrences of the specified set of characters from the input:
tr -d '{{input_characters}}' < {{path/to/file}}
Compress a series of identical characters to a single character:
tr -s '{{input_characters}}' < {{path/to/file}}
Translate the contents of a file to upper-case:
tr "[:lower:]" "[:upper:]" < {{path/to/file}}
Strip out non-printable characters from a file:
tr -cd "[:print:]" < {{path/to/file}}
The examples are provided by the tldr-pages project under the CC BY 4.0 License.
Please note that, as uutils is a work in progress, some examples might fail.