Files
cheatsheet-tldr/tldr/tr
2025-03-27 00:18:38 +00:00

38 lines
1.2 KiB
Plaintext

---
syntax: markdown
tags: [tldr, common]
source: https://github.com/tldr-pages/tldr.git
---
# 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:
`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|--delete]}} '{{input_characters}}' < {{path/to/file}}`
- Compress a series of identical characters to a single character:
`tr {{[-s|--squeeze-repeats]}} '{{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|--complement --delete]}} "[:print:]" < {{path/to/file}}`