Bash tr command

29/12/2020
tr is a very useful UNIX command. It is used to transform string or delete characters from the string. Various type of transformation can be done by using this command, such as searching and replacing text, transforming string from uppercase to lowercase or vice versa, removing repeated characters from the string etc. The command can be used for some complicated transformation also. The different uses of tr command are shown in this tutorial.

Syntax:

tr [option] stringValue1 [stringValue2]

option and stringValue2 are optional for tr command. You can use -c, -s and -d option with tr command to do different types of tasks.

Example-1: Change case

You can change the case of the string very easily by using tr command. To define uppercase, you can use [:upper:] or [A-Z] and to define lowercase you can define [:lower:] or [a-z].

tr command can be used in the following way to convert any string from uppercase to lowercase.

tr [:upper:] [:lower:]

You can use tr command in the following way also to convert any string from lowercase to uppercase.

tr a-z A-Z

Run the following command to convert every small letter of the string,’linuxhint’ into the capital letter.

$ echo linuxhint | tr [:lower:] [:upper:]

You can apply tr command for converting the content of any text file from upper to lower or lower to upper. Suppose, you have text file named, items.txt with the following contents.

  1. Monitor
  2. Keyboard
  3. Mouse
  4. Scanner
  5. HDD

Run the following commands from the terminal to display the content of items.txt and the output of tr command after converting the content of that file from lower to upper case. The following tr command will not modify the original content of the file.

$ cat items.txt
$ tr a-z A-Z < items.txt

You can run the following command to store the output of the tr command into another file named ‘output.txt’.

$ tr [:upper:] [:lower:] < items.txt > output.txt
$ cat output.txt

Example-2: Translate character

tr command can be used to search and replace any particular character from any text. The following command is used to convert each space of the text, “Welcome to Linuxhint” by newline (n).

$ echo "Welcome To Linuxhint" | tr [:space:] ‘n’

Example-3: Using –c option

tr command can be used with -c option to replace those characters with the second character that don’t match with the first character value. In the following example, tr command is used to search those characters in the string ‘bash’ that don’t match with the character ‘b’ and replace them by ‘a’. The output is ‘baaaa’. Four characters are converted here. These are ,’a’,’s’,’h’ and ‘n’.

$ echo "bash" | tr -c ‘b’ ‘a’

Example-4: Using –s option

tr command uses –s option for search and replace any string from a text. In the following example, space (‘ ‘) is replaced by tab (‘t’).

$ echo "BASH Programming" | tr -s ‘ ‘ ‘t’

You can use both -c and -s options together with tr command. In the following example, the range of small letter is used as the first string value. For –c option, tr command will search and replace each capital letter by newline (‘n’) of the file, items.txt and store the output of the command in the file, output.txt.

$ cat items.txt
$ tr -cs [a-z] "n" < items.txt > output.txt
$ cat output.txt

Example-5: Using –d option

-d option used with tr command to search and delete any character or string from a text.  In the following example, tr command will search ‘P’, ‘y’ and ‘t’ in the string “Python is a Programming language” and delete those characters.

$ echo "Python is a Programming language" | tr -d ‘Pyt’

-c option can be used with –d option in the tr command to complement the search like precious –cs command. In the following example, tr command with –cd will search all non-digit characters from the string, “Phone No: 985634854” and delete them.

$ echo "Phone No: 985634854" | tr -cd ‘0-9’

In a similar way, you can run use -cd option in tr command like the following command to remove the non-printable characters from a file. No nonprintable character exists in items.txt. So the output will be the same as the file content.

$ tr -cd "[:print:]" < items.txt

Conclusion

The basic uses of tr command are explained here by using various examples. Hope, this tutorial will help you to learn the purposes of using this command.

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

How to Search for your Files on the Linux Command Line

For a Linux desktop, a user can easily install an app to search their files and folders in the file system, but another...
29/12/2020

Bash Wildcard Tutorial

When we need to search for anything using shell commands then we need to define a pattern for searching. Wildcard characters...
29/12/2020

String concatenation in bash

The way of joining two or more strings together is called string concatenation. It is a common requirement of any programming...
29/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

Huớng dẫn dùng proxy cho ios, iphone 2023
23/09/2023

Cách gắn set proxy cho điện thoại android, oppo, giả lập android, Ldplayer Bằng Proxydroid
20/09/2023

Mua Proxy Socks5 VN Chơi Game Gia Lập Tăng Cường Trải Nghiệm Chơi Game
22/06/2023

Mua Proxy Mỹ, Us Nuôi Tài Khoản Etsy, eBay Tìm Hiểu Về Mua Proxy Mỹ tại Onet.com.vn
22/06/2023

Mua Proxy Game – Giải pháp tuyệt vời cho việc chơi game trên mạng mà không bị giới hạn về vị trí địa lý
03/06/2023