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 Linux tr command was written by Douglas McIlroy. It was introduced in Version 4 Unix.

Syntax

tr [OPTION]… SET1 [SET2]

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

  • Convert lower case to upper case
$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 https://allabouttesting.org 7654321" | tr -d [:digit:]

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

5 minutes tutorial: Learn Linux tr Command with Examples

  • Squeeze more number of spaces into single space

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

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

  • Complement the set using -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 extremely useful command in formatting input and get the results in 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.