5 minutes tutorial: Learn Linux tr Command with Examples

Linux tr command is used for translating and deleting characters. This command is extremely useful for formatting the text in the desired format. The original version of the Linux tr command was written by Douglas McIlroy. It was introduced in Version 4 Unix. This blog provides you with a short tutorial to learn tr command with examples.

Syntax

tr [OPTION]… SET1 [SET2]

  • Delete specific characters
echo 'Please subscribe http://allabouttesting.org' | tr -d 'P'

  • Convert lowercase to uppercase
$echo "please visit my site, thank you" | tr “[a-z]” “[A-Z]”
$echo "please visit my site, thank you" | tr “[:lower:]” “[:upper:]”

  • Convert upper case to lower case
$echo "Thank You For VISITING HTTPS://ALLABOUTTESTING.ORG" | tr “[a-z]” “[A-Z]
$echo "Thank You For VISITING HTTPS://ALLABOUTTESTING.ORG" | tr “[:upper:]” “[:lower:]"

  • Remove all the numerals from the string
$echo "1234567 http://allabouttesting.org 7654321" | tr -d [:digit:]

  • Translate braces into parenthesis
$echo "(http://allabouttesting.org)" | tr '()' '{}'

5 minutes tutorial: Learn Linux tr Command with Examples

  • Squeeze more spaces into a single space

$ echo "Thank you for visiting website" | tr -s [:space:] ' '

  • Print the PATH directory location on a separate line
$echo $PATH | tr  ":" "\n"

  • Complement the set using the -c option
$echo "12345678 https://allaboutesting.org 87654321" | tr -cd "[:alpha:]" 
\n -> used to change the line while displaying output

Conclusion

Linux tr command is an extremely useful command in formatting input and getting the results in the desired format. I have used this tool extensively in my day-to-day work and it helps me a lot.

Subscribe us to receive more such articles updates in your email.

If you have any questions, feel free to ask in the comments section below. Nothing gives me greater joy than helping my readers!

Disclaimer: This tutorial is for educational purpose only. Individual is solely responsible for any illegal act.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

10 Blockchain Security Vulnerabilities OWASP API Top 10 - 2023 7 Facts You Should Know About WormGPT OWASP Top 10 for Large Language Models (LLMs) Applications Top 10 Blockchain Security Issues