Linux tr Command Tutorial With Examples – POFTUT

Linux tr Command Tutorial With Examples

tr command is mainly used to translate or delete characters in bash. tr is not a complex but useful command. We will look at various usage scenarios of tr in this tutorial.

Syntax

We will use the following syntax for tr command.

Help

Help can get with --help parameter like below.

Help

Help

Man

The manpage of tr can be also viewed below.

Man

Man

Convert Lowercase To Uppercase

One of the most popular usages of tr is converting texts from lowercase to uppercase.

Convert Lower Case To Upper Case

Convert Lowercase To Uppercase

Convert Uppercase To Lowercase

Reverse application of Lowercase to uppercase is uppercase to lowercase. This can be implemented with the command below.

OR

Convert Upper Case To Lower Case

Convert Uppercase To Lowercase

Translate Braces To Parenthesis

We can translate braces into parenthesis like below. This can be applied to other characters too.

Translate Braces To Parenthesis

Translate Braces To Parenthesis

Translate White-space To Tabs

White spaces can be expressed like below and translated to the tabs with the following command.

Translate White-space To Tabs

Translate White-space To Tabs

Delete Specified Characters

Another useful feature is deleting specified characters from the given text. The character that will be deleted is specified with -d parameter like below.

Delete Specified Characters

Delete Specified Characters

Complement Characters

The filtering is another feature of tr. Only Specified characters can be printed like below. In this example, we will only print numbers or digits with the following command.

LEARN MORE  How To Grep Text Files With Powershell Grep or Select-String Cmdlet In Windows?

Complement Characters

Complement Characters

Remove All Non-printable Characters

Non-printable characters can be removed with the following command with-cd [:print:]

Remove All Non-printable Characters

Remove All Non-printable Characters

Join Multiple Line Into Single

The following command will translate line endings into spaces and make the whole multi-line text a single line text.

Join Multiple Line Into Single

Join Multiple Line Into Single

Expressions

All supported expressions can be found below.

1 Response

Leave a Reply

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

Enjoy this blog? Please spread the word :)