2015-03-12 08:41:54 +00:00
|
|
|
# tr
|
|
|
|
|
2019-01-11 11:09:02 +00:00
|
|
|
> Translate characters: run replacements based on single characters and character sets.
|
2021-03-30 14:46:31 +01:00
|
|
|
> More information: <https://www.gnu.org/software/coreutils/manual/html_node/tr-invocation.html>.
|
2015-03-12 08:41:54 +00:00
|
|
|
|
2016-01-07 17:31:27 +00:00
|
|
|
- Replace all occurrences of a character in a file, and print the result:
|
2015-03-12 08:41:54 +00:00
|
|
|
|
2019-01-11 11:09:02 +00:00
|
|
|
`tr {{find_character}} {{replace_character}} < {{filename}}`
|
|
|
|
|
|
|
|
- Replace all occurrences of a character from another command's output:
|
|
|
|
|
|
|
|
`echo {{text}} | tr {{find_character}} {{replace_character}}`
|
2015-03-12 08:41:54 +00:00
|
|
|
|
2016-01-07 17:31:27 +00:00
|
|
|
- Map each character of the first set to the corresponding character of the second set:
|
2015-03-14 11:05:19 +00:00
|
|
|
|
2019-01-11 11:09:02 +00:00
|
|
|
`tr '{{abcd}}' '{{jkmn}}' < {{filename}}`
|
2015-03-14 11:05:19 +00:00
|
|
|
|
2017-05-12 10:29:18 +01:00
|
|
|
- Delete all occurrences of the specified set of characters from the input:
|
2015-03-14 11:05:19 +00:00
|
|
|
|
2019-01-11 11:09:02 +00:00
|
|
|
`tr -d '{{input_characters}}' < {{filename}}`
|
2015-03-14 11:05:19 +00:00
|
|
|
|
2016-01-07 17:31:27 +00:00
|
|
|
- Compress a series of identical characters to a single character:
|
2015-03-14 11:05:19 +00:00
|
|
|
|
2019-01-11 11:09:02 +00:00
|
|
|
`tr -s '{{input_characters}}' < {{filename}}`
|
2015-03-14 11:05:19 +00:00
|
|
|
|
2019-01-11 11:09:02 +00:00
|
|
|
- Translate the contents of a file to upper-case:
|
2015-03-12 08:41:54 +00:00
|
|
|
|
|
|
|
`tr "[:lower:]" "[:upper:]" < {{filename}}`
|
|
|
|
|
2019-01-11 11:09:02 +00:00
|
|
|
- Strip out non-printable characters from a file:
|
2015-03-12 08:41:54 +00:00
|
|
|
|
|
|
|
`tr -cd "[:print:]" < {{filename}}`
|