Explain TR Command
Explain TR Command
There are many reasons for translating characters from one set to another. One of the most
common is to convert lowercase characters to uppercase, or vice versa. UNIX provides a translate
utility making conversions from one set to another.
tr Command:
The tr command is a UNIX command-line utility for translating or deleting characters. It supports
a range of transformations including
1. uppercase to lowercase,
2. squeezing repeating characters,
3. deleting specific characters, and
4. Basic find and replace.
The strings are specified using quotes. The format for the translate filter is shown in Figure . Note
that only the keyboard is specified. Translate will not accept data from a file. To translate a file,
therefore, we must redirect the file into the translate command.
Syntax :
$ tr [OPTION] [SET1] [SET2]
Examples:
1. How to convert lower case characters to upper case.
To convert characters from lower case to upper case, you can either specify a range of characters
or use the predefined character classes.
Syntax :
$cat filename.txt |tr [:lower:] [:upper] or $cat filename.txt |tr [a-z] [A-Z]
$ cat>t4.txt<= is a file
Input :( Good morning everyone i am your eswi faculty p shanker naik)
sir from cvr college
Output: {Good morning everyone i am your eswi faculty p shanker naik}
sir from cvr college
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
Syntax:
$cat filename |tr –s “ “
Input:$ echo "Welcome To all B.Tech First Year Students" | tr -s “ “
Output: Wellcome To all B.tech First Year Students
You can complement the SET1 using -c option. For example, to remove all characters except
digits, you can use the following.
Syntax:
1.$cat filename | tr –cd [:digit:],
2.$cat filename | tr -c tr -c "enter the same characters in side quotes" "*" <filename
Example:01.$cat t8.txt
Output: 245naik1231@DESKTOP-BG36MNF:~$
Example:02,$cat>srt.txt
Output: i**i***e***ea****o*u*e*********naik1231