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-04-01 16:54:26 +01:00
|
|
|
> More information: <https://www.gnu.org/software/coreutils/tr>.
|
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
|
|
|
|
2022-12-06 06:47:56 +00:00
|
|
|
`tr {{find_character}} {{replace_character}} < {{path/to/file}}`
|
2019-01-11 11:09:02 +00:00
|
|
|
|
|
|
|
- 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
|
|
|
|
2022-12-06 06:47:56 +00:00
|
|
|
`tr '{{abcd}}' '{{jkmn}}' < {{path/to/file}}`
|
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
|
|
|
|
2022-12-06 06:47:56 +00:00
|
|
|
`tr -d '{{input_characters}}' < {{path/to/file}}`
|
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
|
|
|
|
2022-12-06 06:47:56 +00:00
|
|
|
`tr -s '{{input_characters}}' < {{path/to/file}}`
|
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
|
|
|
|
2022-12-06 06:47:56 +00:00
|
|
|
`tr "[:lower:]" "[:upper:]" < {{path/to/file}}`
|
2015-03-12 08:41:54 +00:00
|
|
|
|
2019-01-11 11:09:02 +00:00
|
|
|
- Strip out non-printable characters from a file:
|
2015-03-12 08:41:54 +00:00
|
|
|
|
2022-12-06 06:47:56 +00:00
|
|
|
`tr -cd "[:print:]" < {{path/to/file}}`
|