The Wayback Machine - https://web.archive.org/web/20250306005205/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
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

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

Master DevOps and also get 90% Course fee refund on completing 90% course in 90 days! Take the Three 90 Challenge today.

The most sought-after Three 90 challenge has started and this is your chance to upskill and get 90% refund. What more motivation do you need? Start the challenge right away!


Next Article

Similar Reads

Ccat – Colorize Cat Command Output command in Linux with Examples
ccat is a Linux and macOS cat command that improved its precursor: the syntax highlighted file contents can be displayed with its help. Therefore, in the case of source code and configuration files, it is extremely helpful. It can highlight over 10 languages, like JavaScript, Java, Ruby, Python, Go, C, and JSON.Syntaxccat [OPTIONS] [FILE...]Basic U
3 min read
How to Display Command History in Linux | history Command
The history command in Linux is one of the most essential tools especially for those who prefer working with the Linux terminal. This powerful command allows users to view, search, and manipulate the list of previously executed commands. Mastering the history command in Linux helps you efficiently reuse commands, automate repetitive tasks, and trou
3 min read
Tail command in Linux with examples
It is the complementary of head command. The tail command, as the name implies, prints the last N number of data of the given input. By default, it prints the last 10 lines of the specified files. If more than one file name is provided then data from each file is preceded by its file name. Syntax of Tail Command in Linuxtail [OPTION]... [FILE]... L
7 min read
wc command in Linux with examples
wc stands for word count. As the name implies, it is mainly used for counting purpose.It is used to find out number of lines, word count, byte and characters count in the files specified in the file arguments.By default it displays four-columnar output.First column shows number of lines present in a file specified, second column shows number of wor
6 min read
bc command in Linux with examples
bc command is used for command line calculator. It is similar to basic calculator by using which we can do basic mathematical calculations. Arithmetic operations are the most basic in any kind of programming language. Linux or Unix operating system provides the bc command and expr command for doing arithmetic calculations. You can use these command
11 min read
AWK command in Unix/Linux with examples
Awk is a scripting language used for manipulating data and generating reports. The awk command programming language requires no compiling and allows the user to use variables, numeric functions, string functions, and logical operators. Awk is a utility that enables a programmer to write tiny but effective programs in the form of statements that def
8 min read
Paste command in Linux with examples
Paste command is one of the useful commands in Unix or Linux operating system. It is used to join files horizontally (parallel merging) by outputting lines consisting of lines from each file specified, separated by tab as delimiter, to the standard output. When no file is specified, or put dash ("-") instead of file name, paste reads from standard
6 min read
rm command in Linux with examples
rm stands for remove here. rm command is used to remove objects such as files, directories, symbolic links and so on from the file system like UNIX. To be more precise, rm removes references to objects from the filesystem, where those objects might have had multiple references (for example, a file with two different names). By default, it does not
5 min read
uname command in Linux with Examples
Linux, renowned for its open-source flexibility and powerful performance, offers a range of commands that reveal the inner workings of your system. Among these, the 'uname' command stands out as a versatile tool that provides key details about your Linux machine. Here, we will learn the basics of the 'uname' command, its various options, and how it
4 min read
cal command in Linux with Examples
The 'cal' command in Linux is a versatile tool that displays calendars directly in the terminal. If a user wants a quick view of the calendar in the Linux terminal, 'cal' is the command for you. Here’s a look at the usage and features of 'cal' command in Linux.What is the 'cal' command?cal command is a calendar command in Linux which is used to see
2 min read