![]() |
VOOZH | about |
The tr (translate) command in Linux is used to transform or delete specific characters from text input. It is commonly used for tasks like changing letter cases, removing unwanted characters, or compressing repeating characters.
Suppose we have a file named test.txt that contains both lowercase and uppercase text. We can use the tr command to convert all lowercase characters to uppercase.
cat test.txtOutput:
It is often used together with pipes (|) or redirection (>>) to process the content of files in more complex ways.
tr [OPTION] SET1 [SET2]
Options
-c : Applies the operation to characters not in the specified set (complements the set).-d : Deletes all characters in the first set from the output.-s : Replaces repeated occurrences of characters in set1 with a single occurrence.-t : Truncates set1 to match the length of set2 during translation.cat test.txt | tr [a-z] [A-Z]Output:
cat test.txt | tr [:lower:] [:upper:]Output:
WELCOME TO
GEEKSFORGEEKSExample 3: Using input redirection
tr [:lower:] [:upper:] < test.txtOutput:
WELCOME TO
GEEKSFORGEEKS[a-z] [A-Z] converts lowercase letters to uppercase.[:lower:] [:upper:] does the same but is more portable across systems.Here are the commonly used options in the tr command in Linux, along with examples and short explanations
The following command translates all the white-space characters to tabs:
echo "Welcome To GeeksforGeeks" | tr [:space:] "\t"
Output:
Welcome To GeeksforGeeksIn the previous example we can also use redirection to provide intput for tr. Although this time we will use a here string for that:
tr [:space:] "\t" <<< "Welcome To GeeksforGeeks"
Output:
Welcome To GeeksforGeeksYou can also translate from and to a file. In this example we will translate braces in a file with parenthesis.
$ cat greekfileOutput:
{WELCOME TO}
GeeksforGeeks$ tr "{}" "()" <greekfile >newfile.txtOutput:
(WELCOME TO)
GeeksforGeeksThe above command will read each character from “geekfile.txt”, translate if it is a brace, and write the output to “newfile.txt”.
To squeeze repetitive occurrences of characters specified in a set use the -s option. This removes repeated instances of characters of the last SET specified. OR we can say that, you can convert multiple continuous spaces with a single space:
$ echo "Welcome To GeeksforGeeks" | tr -s " "
Output:
Welcome To GeeksforGeeksAnd again, accomplish the same task but using a string here:
tr -s " " <<< "Welcome To GeeksforGeeks"
Output:
Welcome To GeeksforGeeksTo delete specific characters use the -d option. This option deletes characters in the first set specified.
echo "Welcome To GeeksforGeeks" | tr -d WOutput:
elcome To GeeksforGeeksOr equivalently use:
tr -d W <<< "Welcome to GeeksforGeeks"Output:
elcome To GeeksforGeeksYou can use:
echo "my ID is 73535" | tr -d [:digit:]or
tr -d [:digit:] <<< "my ID is 73535"You can complement the SET1 using -c option. For example, to remove all characters except digits, you can use the following.
$ echo "my ID is 73535" | tr -cd [:digit:]
or
$ tr -cd [:digit:] <<< "my ID is 73535"
Output:
73535&list=PLqM7alHXFySFc4KtwEZTANgmyJm3NqS_L&ab_channel=GeeksforGeeks