# tr Command
The `tr` command is used to translate or delete characters in a file. It can read from a file or from the standard input, and after the string translation, it outputs the result to the standard output.
## Syntax
```shell
tr [OPTION]... SET1 [SET2]
-c, -C, --complement
: use the complement ofSET1
.-d, --delete
: delete characters inSET1
without translation.-s, --squeeze-repeats
: replace each input sequence of a repeated character listed inSET1
with a single occurrence of that character.-t, --truncate-set1
: first truncateSET1
to the length ofSET2
.--help
: display this help and exit.--version
: output version information and exit.
... (The content of this section remains unchanged)
The content of the file.txt
file is as follows.
Hello World
Convert all letters in the file to uppercase.
cat file.txt | tr [a-z] [A-Z]
# HELLO WORLD
You can achieve the same using the [:lower]
and [:upper]
arguments.
cat file.txt | tr [:lower:] [:upper:]
# HELLO WORLD
Convert whitespace characters to tab.
cat file.txt | tr [:space:] "\t"
# Hello World
Delete all o
characters.
cat file.txt | tr -d "o"
# Hell Wrld
Delete all numbers.
echo "My ID is 73535" | tr -d [:digit:]
# My ID is
Extract numbers from a string.
echo "My ID is 73535" | tr -cd [:digit:]
# 73535
https://github.com/WindrunnerMax/EveryDay
https://www.runoob.com/linux/linux-comm-tr.html
https://www.tutorialspoint.com/unix_commands/tr.htm
https://www.geeksforgeeks.org/tr-command-in-unix-linux-with-examples/