DEV Community

Cover image for Basic Linux command (tr)
Cheulong Sear
Cheulong Sear

Posted on

Basic Linux command (tr)

tr is used to translate, squeeze, and/or delete characters from standard input, writing to standard output.

tr command is usually used alone side with other command.

echo "Hello World" | tr [a-z] [A-Z]
# or echo "Hello World" | tr [:lower:] [:upper:]
----
# output:
HELLO WORLD
Enter fullscreen mode Exit fullscreen mode
echo "Hello World!" | tr "!" "."
----
# output:
Hello World.
Enter fullscreen mode Exit fullscreen mode

You can also delete character with tr command

echo "Hello World" | tr -d [a-z]

----
# output:
H W
Enter fullscreen mode Exit fullscreen mode

Another feature is squeeze, we can remove the duplicate character

echo "localhostt" | tr -s "t"

----
# output:
localhost
Enter fullscreen mode Exit fullscreen mode

Other arguement

[:alnum:] all letters and digits
[:alpha:] all letters
[:blank:] all horizontal whitespace
[:digit:] all digits
[:graph:] all printable characters, not including space
[:lower:] all lower case letters
[:print:] all printable characters, including space
[:punct:] all punctuation characters
[:space:] all horizontal or vertical whitespace
[:upper:] all upper case letters

Leave a comment if you have any questions.

===========
Please keep in touch
Portfolio
Linkedin
Github
Youtube

Top comments (0)