2025-03-14 21:59:59 -06:00
|
|
|
# tr
|
|
|
|
|
|
|
|
|
|
> Translate characters: run replacements based on single characters and character sets.
|
|
|
|
|
> More information: <https://www.gnu.org/software/coreutils/manual/html_node/tr-invocation.html>.
|
|
|
|
|
|
|
|
|
|
- Replace all occurrences of a character in a file, and print the result:
|
|
|
|
|
|
2025-12-16 10:20:31 -07:00
|
|
|
`tr < {{path/to/file}} {{find_character}} {{replace_character}}`
|
2025-03-14 21:59:59 -06:00
|
|
|
|
|
|
|
|
- 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:
|
|
|
|
|
|
2025-12-16 10:20:31 -07:00
|
|
|
`tr < {{path/to/file}} '{{abcd}}' '{{jkmn}}'`
|
2025-03-14 21:59:59 -06:00
|
|
|
|
|
|
|
|
- Delete all occurrences of the specified set of characters from the input:
|
|
|
|
|
|
2025-12-16 10:20:31 -07:00
|
|
|
`tr < {{path/to/file}} {{[-d|--delete]}} '{{input_characters}}'`
|
2025-03-14 21:59:59 -06:00
|
|
|
|
|
|
|
|
- Compress a series of identical characters to a single character:
|
|
|
|
|
|
2025-12-16 10:20:31 -07:00
|
|
|
`tr < {{path/to/file}} {{[-s|--squeeze-repeats]}} '{{input_characters}}'`
|
2025-03-14 21:59:59 -06:00
|
|
|
|
|
|
|
|
- Translate the contents of a file to upper-case:
|
|
|
|
|
|
2025-12-16 10:20:31 -07:00
|
|
|
`tr < {{path/to/file}} "[:lower:]" "[:upper:]"`
|
2025-03-14 21:59:59 -06:00
|
|
|
|
|
|
|
|
- Strip out non-printable characters from a file:
|
|
|
|
|
|
2025-12-16 10:20:31 -07:00
|
|
|
`tr < {{path/to/file}} {{[-cd|--complement --delete]}} "[:print:]"`
|