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

NFS Server and Client Installation on CentOS 7

1 Preliminary Note I have fresh installed CentOS 7 server, on which I am going to install the NFS server. My CentOS server...
01/11/2021

Bash Alias Tutorial

How to use bash aliases Most of the users like to use shortcuts for running commands. There are many commands in Ubuntu...
28/12/2020

Bash text and background printing in different colors

A terminal is a very important application for any Linux operating system. It is mainly used to execute different commands...
29/12/2020
Bài Viết

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

Reliable IPv4 and IPv6 Subnet Rental Services: The Perfect Solution for Global Businesses
23/12/2024

Tìm Hiểu Về Thuê Proxy US – Lợi Ích và Cách Sử Dụng Hiệu Quả
11/12/2024

Mua Proxy V6 Nuôi Facebook Spam Hiệu Quả Tại Onetcomvn
03/06/2024

Hướng dẫn cách sử dụng ProxyDroid để duyệt web ẩn danh
03/06/2024

Mua proxy Onet uy tín tại Onet.com.vn
03/06/2024