tr [option] stringValue1 [stringValue2]
option and stringValue2 are optional for tr command. You can use -c, -s and -d option with tr command to do different types of tasks.
Example-1: Change case
You can change the case of the string very easily by using tr command. To define uppercase, you can use [:upper:] or [A-Z] and to define lowercase you can define [:lower:] or [a-z].
tr command can be used in the following way to convert any string from uppercase to lowercase.
tr [:upper:] [:lower:]
You can use tr command in the following way also to convert any string from lowercase to uppercase.
tr a-z A-Z
Run the following command to convert every small letter of the string,’linuxhint’ into the capital letter.
You can apply tr command for converting the content of any text file from upper to lower or lower to upper. Suppose, you have text file named, items.txt with the following contents.
Run the following commands from the terminal to display the content of items.txt and the output of tr command after converting the content of that file from lower to upper case. The following tr command will not modify the original content of the file.
$ tr a-z A-Z < items.txt
You can run the following command to store the output of the tr command into another file named ‘output.txt’.
$ cat output.txt
Example-2: Translate character
tr command can be used to search and replace any particular character from any text. The following command is used to convert each space of the text, “Welcome to Linuxhint” by newline (\n).
Example-3: Using –c option
tr command can be used with -c option to replace those characters with the second character that don’t match with the first character value. In the following example, tr command is used to search those characters in the string ‘bash’ that don’t match with the character ‘b’ and replace them by ‘a’. The output is ‘baaaa’. Four characters are converted here. These are ,’a’,’s’,’h’ and ‘\n’.
Example-4: Using –s option
tr command uses –s option for search and replace any string from a text. In the following example, space (‘ ‘) is replaced by tab (‘\t’).
You can use both -c and -s options together with tr command. In the following example, the range of small letter is used as the first string value. For –c option, tr command will search and replace each capital letter by newline (‘\n’) of the file, items.txt and store the output of the command in the file, output.txt.
$ tr -cs [a-z] "\n" < items.txt > output.txt
$ cat output.txt
Example-5: Using –d option
-d option used with tr command to search and delete any character or string from a text. In the following example, tr command will search ‘P’, ‘y’ and ‘t’ in the string “Python is a Programming language” and delete those characters.
-c option can be used with –d option in the tr command to complement the search like precious –cs command. In the following example, tr command with –cd will search all non-digit characters from the string, “Phone No: 985634854” and delete them.
In a similar way, you can run use -cd option in tr command like the following command to remove the non-printable characters from a file. No nonprintable character exists in items.txt. So the output will be the same as the file content.
The basic uses of tr command are explained here by using various examples. Hope, this tutorial will help you to learn the purposes of using this command.