1. Explain tr command with examples?
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]
Figure and Options :
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]
2. How to translate white-space characters to tabs?
The following command translates all the white-space characters to tabs
Syntax :
$cat filename.txt |tr [:space:] “\t”
$ echo "Welcome To CSE-G Students" | tr [:space:] "\t“
We can also use redirection to provide input for tr. Although this time we will use a here string
for that:
Redirection Syntax:
$tr [:space:] "\t" <<< " Welcome To CSE-G Students “
3. How to translate braces into parenthesis?
You can also translate from and to a file. In this example we will translate braces in a file
with parenthesis.
Syntax:
$ tr "{ }" "( )" <t4.txt(this is old file) >t5.txt=> (newfile.txt)
$ 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
4. How to squeeze a sequence of repetitive characters using -s option.?
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
5. How to delete specified characters using -d option.?
To delete specific characters use the -d option. This option deletes characters in the first set
specified.
Syntax:
$cat filename | tr -d characters
name {Good morning everyone i am your eswi faculty p shanker naik}
Example(Input):
sir from cvr college
$cat t5.txt | tr -d G
Output:{ood morning everyone i am your eswi faculty p shanker naik}
sir from cvr college
6. How to complement the sets using –c and cd option?
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
Syntax: $cat t8.txt | tr –cd 246
Input: MY FACULTY ID IS CVRCSEF246
Output: 245naik1231@DESKTOP-BG36MNF:~$
Example:02,$cat>srt.txt
Input: it is very easy to use SHANKER
Syntax:$tr -c "aeiou" "*" <srt.txt
Output: i**i***e***ea****o*u*e*********naik1231