0% found this document useful (0 votes)
6 views3 pages

Understanding the tr Command in UNIX

Explain abt tr commands
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Understanding the tr Command in UNIX

Explain abt tr commands
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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 [Link] |tr [:lower:] [:upper] or $cat [Link] |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 [Link] |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 "{ }" "( )" <[Link](this is old file) >[Link]=> ([Link])

 $ cat>[Link]<= 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 [Link] First Year Students" | tr -s “ “
 Output: Wellcome To all [Link] 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 [Link] | 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 [Link]

Syntax: $cat [Link] | tr –cd 246

Input: MY FACULTY ID IS CVRCSEF246

Output: 245naik1231@DESKTOP-BG36MNF:~$

 Example:02,$cat>[Link]

Input: it is very easy to use SHANKER

Syntax:$tr -c "aeiou" "*" <[Link]

Output: i**i***e***ea****o*u*e*********naik1231

Common questions

Powered by AI

The 'tr' command caters to the deletion of characters through the -d option, which requires specifying the characters in the first set to be removed from the input stream. The command `$ cat filename | tr -d 'characters'` effectively deletes all instances of 'characters' from the file. This targeted deletion provides a streamlined method for data cleanup or formatting, ensuring that specific undesired elements are efficiently removed without affecting the overall content structure .

The '-d' option of the 'tr' command is used to delete specified characters from the input text. This can be useful when cleaning up a dataset or a text by removing unwanted characters. For instance, if you have a text file containing various information and you need to remove all instances of the letter 'G', you could use the command `$cat t5.txt | tr -d G`, which will delete the character 'G' from the text. This helps in refining text data for further analysis or processing .

The 'tr' command can be used to alter braces to parentheses by specifying the braces as the first set and parentheses as the second set. By using the syntax `$ cat t4.txt | tr "{}" "()"`, braces within the input text are translated into parentheses. This transformation might be useful in scenarios where the textual representation must conform to a specific syntax requirement, such as formatting data for compatibility with programming languages that use different symbols for grouping or demarcation .

Complementing character sets in the 'tr' command involves using the -c option to invert the specified set, thereby selecting characters not in the specified set for action. To remove all vowels from a string, you can complement the set of vowels, using a command like `$ tr -c "aeiou" '*' <srt.txt`, which replaces non-vowel characters with '*'. The effect is that only vowels are retained, demonstrating the utility of complemented sets in filtering or targeting specific text components for transformation or exclusion .

The 'tr' command handles translation of character ranges by specifying ranges in square brackets, allowing the conversion of characters within the specified range from one set to another. An example of converting text to uppercase is the command `$ cat filename.txt | tr [a-z] [A-Z]`, which translates lowercase letters to uppercase across the entire file. This functionality is particularly useful in standardizing text data for analysis or display purposes, ensuring consistency in the textual dataset .

Quotes in the 'tr' command syntax are significant as they explicitly define the character strings to be translated. This ensures accurate interpretation of spaces and special characters, avoiding unintended omissions or misinterpretations. When you encapsulate sets of characters in quotes, you maintain the integrity of text processing, accurately reflecting user intention in operations such as replacing spaces or translating specific sequences within a text stream .

The 'tr' command can translate whitespace characters to tabs by specifying the whitespace character set using `[:space:]` and replacing it with a tab character represented as `\t`. An example command for this translation is `$ echo "Welcome To CSE-G Students" | tr [:space:] "\t"`, which converts all whitespace characters in the string to tabs, effectively formatting the text for use in environments where tab-delimited data is required .

The 'tr' command in UNIX is used for translating and deleting characters in text streams. Its primary functions include converting lowercase characters to uppercase (and vice versa), squeezing repeating characters into a single character using the -s option, deleting specific characters using the -d option, and performing basic find and replace operations. These functions are beneficial in text processing as they allow for efficient manipulation of text data, enabling quick transformations and cleanups of datasets without needing more complex scripting or programming tools .

The 'tr' command can complement sets using the -c option, which allows for the selection of characters that are not in the specified set. This is significant when you want to operate on or exclude everything except for a particular set of characters. For example, if you need to remove all characters except digits from a text, you can use the command `$ cat t8.txt | tr –cd [:digit:]`, which complements the set to only include digits, subsequently deleting all non-digit characters. This is useful for extracting numeric data from mixed-content files .

The 'tr' command uses the -s option to squeeze sequences of repeated characters into a single character. This is particularly useful in managing whitespace or eliminating redundant characters within a text stream. For example, using the command `$ echo "Welcome To all B.Tech First Year Students" | tr -s " "` results in squeezing multiple spaces into a single space, which helps in formatting text for better readability and processing .

You might also like