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

Bash exit on error

An exit status code is returned when any Linux command is executed from the terminal, either the command is successful...
29/12/2020

Bash Comments

How to use single and multiple line comments in BASH Using comments in any script or code is very important to make the...
28/12/2020

Bash Environment Variables Tutorial

Overview Whenever we talk to a remote server or do anything on a shell, the shell stores data in form of variables which...
28/12/2020
Bài Viết

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

Mua proxy v4 chạy socks5 để chơi game an toàn, tốc độ cao ở đâu?
18/05/2024

Thuê mua proxy Telegram trọn gói, tốc độ cao, giá siêu hời
18/05/2024

Thuê mua proxy Viettel ở đâu uy tín, chất lượng và giá tốt? 
14/05/2024

Dịch vụ thuê mua proxy US UK uy tín, chất lượng số #1
13/05/2024

Thuê mua proxy Việt Nam: Báo giá & các thông tin MỚI NHẤT
13/05/2024