Change characters

The tr command in Linux is used to change characters. Some examples of its usage:

  • convert lowercase characters to uppercase
  • translate braces into parentheses
  • delete characters

The syntax of the tr command is: tr [OPTIONS] SET1 [SET2]. The characters you want to change are specified with the SET1 argument, while the characters that will replace them are specified with the SET2 argument. To pass a file to this command, the input redirection operator (<) must be used.

Let’s use this command in a couple of ways. Our example file named tr_example has the following content:
This is a text. More text. This is a sentence.

To change every occurence of the letter a in the file into the uppercase A , we can use the following command:

tr command example

We can specify more than one character to change:

tr command more characters

To convert each character to uppercase, we can use the tr [:lower:] [:upper:] command:

tr command uppercase

To remove a particular characer, we can use the -d option:

tr command delete characters

Geek University 2022