The Wayback Machine - https://web.archive.org/web/20240728210501/https://www.geeksforgeeks.org/tr-command-in-unix-linux-with-examples/
Open In App

tr command in Unix/Linux with examples

Last Updated : 12 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

The tr command is a UNIX command-line utility for translating or deleting characters. It supports a range of transformations including uppercase to lowercase, squeezing repeating characters, deleting specific characters, and basic find and replace. It can be used with UNIX pipes to support more complex translation. tr stands for translate. 

Syntax :

$ tr [OPTION] SET1 [SET2]

Options -c : complements the set of characters in string.i.e., operations apply to characters not in the given set -d : delete characters in the first set from the output. -s : replaces repeated characters listed in the set1 with single occurrence -t : truncates set1.

Sample Commands

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. 

$ cat greekfile

Output:

WELCOME TO 
GeeksforGeeks
$ cat greekfile | tr [a-z] [A-Z]

Output:

WELCOME TO
GEEKSFORGEEKS

or

$ cat greekfile | tr [:lower:] [:upper:]

Output:

WELCOME TO
GEEKSFORGEEKS

Alternatively, you can provide input for the tr command using redirection:

tr [:lower:] [:upper:] <greekfile

Output:

WELCOME TO
GEEKSFORGEEKS

2. How to translate white-space characters to tabs. The following command translates all the white-space characters to tabs

$ echo "Welcome To GeeksforGeeks" | tr [:space:] "\t"

Output:

Welcome    To    GeeksforGeeks

In 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    GeeksforGeeks

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.

$ cat greekfile

Output:

{WELCOME TO} 
GeeksforGeeks
$ tr "{}" "()" <greekfile >newfile.txt

Output:

(WELCOME TO) 
GeeksforGeeks

The above command will read each character from “geekfile.txt”, translate if it is a brace, and write the output to “newfile.txt”.

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

$ echo "Welcome    To    GeeksforGeeks" | tr -s " "

Output:

Welcome To GeeksforGeeks

And again, accomplish the same task but using a string here:

tr -s " " <<< "Welcome    To    GeeksforGeeks"

Output:

Welcome To GeeksforGeeks

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.

$ echo "Welcome To GeeksforGeeks" | tr -d W

Output:

elcome To GeeksforGeeks

Or equivalently use:

tr -d W <<< "Welcome to GeeksforGeeks"

Output:

elcome To GeeksforGeeks

6. To remove all the digits from the string, you can use

$ echo "my ID is 73535" | tr -d [:digit:]

or

$ tr -d [:digit:] <<< "my ID is 73535"

Output:

my ID is

7. How to complement the sets using -c option 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

tr command in Unix/Linux with examples – FAQs

What is the tr command in Unix?

The tr command in Unix is used for translating or deleting characters from standard input and writing the result to standard output. It is commonly used for text processing tasks such as converting case, deleting characters, and squeezing repeated characters.

What is tr for Linux?

In Linux, the tr command is a utility for translating or deleting characters. It is used to replace, remove, or compress sequences of characters in a stream.

How to use tr to delete characters?

To delete characters using tr, use the -d option followed by the characters you want to delete. For example, to delete all digits from the input:

echo "hello123" | tr -d '0-9'

Output:

hello

How does tr remove white space in Linux?

To remove white space characters using tr, use the -d option and specify the space character:

echo "hello world" | tr -d ' '

This will remove all spaces from the input. For all white spaces (spaces, tabs, etc.):

echo "hello  world" | tr -d '[:space:]'

How to remove newline in tr command?

To remove newline characters using tr, use the -d option and specify the newline character:

echo -e "hello\nworld" | tr -d '\n'

Output:

helloworld

Previous Article
Next Article

Similar Reads

Ccat – Colorize Cat Command Output command in Linux with Examples
ccat is a command-line tool for Linux and OSX, which is similar to the cat command in Linux. But the difference between cat and ccat is that the ccat shows the content of the file with the syntax highlighted. Currently, ccat supports the following programming languages. JavaScriptJavaRubyPythonGoCJSONInstallation of Ccat First, we are going to see
2 min read
How to Display Command History in Linux | history Command
The command-line interface in Linux provides powerful tools for users, and mastering command history is essential for efficient navigation and retrieval of previously executed commands. The history command is a valuable utility that allows users to view and search through their command history. In this comprehensive guide, we will explore the vario
4 min read
select command in Linux with examples
select command in Linux is used to create a numbered menu from which a user can select an option. If the user enters a valid option then it executes the set of command written in select block and then ask again to enter a number, if a wrong option is entered it does nothing. If user enters nothing and simply press 'enter' the option menu gets print
1 min read
atrm command in Linux with examples
atrm command is used to remove the specified jobs. To remove a job, its job number is passed in the command. A user can only delete jobs that belong to him. Only superuser can delete any job even if that belongs to another user. Syntax: atrm [-V] job [job...] Options: -V : Used to print the version number atrm -V job : Job number of the job which i
1 min read
comm command in Linux with examples
comm compare two sorted files line by line and write to standard output; the lines that are common and the lines that are unique. Suppose you have two lists of people and you are asked to find out the names available in one and not in the other, or even those common to both. comm is the command that will help you to achieve this. It requires two so
5 min read
cmp Command in Linux with examples
cmp command in Linux/UNIX is used to compare the two files byte by byte and helps you to find out whether the two files are identical or not. When cmp is used for comparison between two files, it reports the location of the first mismatch to the screen if difference is found and if no difference is found i.e the files compared are identical.cmp dis
5 min read
expand Command in LINUX with examples
Whenever you work with files in LINUX there can be a situation when you are stuck with a file containing many tabs and whatever you need to do with a file requires that file with no tabs but with spaces. In this situation, the task looks quite simple if you are dealing with a small file but what if the file you are dealing with is very big or you n
3 min read
df Command in Linux with examples
There might come a situation while using Linux when you want to know the amount of space consumed by a particular file system on your LINUX system or how much space is available on a particular file system. LINUX being command friendly provides a command line utility for this i.e df command that displays the amount of disk space available on the fi
8 min read
help Command in Linux with examples
If you are new to LINUX operating system and having trouble dealing with the command-line utilities provided by LINUX then you really need to know first of all about the help command which as its name says help you to learn about any built-in command. help command as told before just displays information about shell built-in commands. Here's the sy
3 min read
fgrep command in Linux with examples
The fgrep filter is used to search for the fixed-character strings in a file. There can be multiple files also to be searched. This command is useful when you need to search for strings which contain lots of regular expression metacharacters, such as "^", "$", etc. Syntax: fgrep [options] [ -e pattern_list] [pattern] [file] Options with Description
3 min read
three90RightbarBannerImg