tr command in Linux with example

In this tutorial, I use the tr command to translate or delete characters. Now, let’s go tr command in Linux for example.

The “tr” command in Linux is a useful utility for translating or deleting characters. It is primarily used for character-level transformations in text files or input streams. The “tr” command replaces or deletes specific characters as per the given rules.

What does tr command mean?

tr – translate or delete characters

Syntax

tr [OPTION]... [INPUT [OUTPUT]]

On the man page, the describes it

  • tr – translate or delete characters.
  • man tr – More details information about tr command.
tr command in Linux with example

tr command in Linux with an example

I have created a file tr_command.txt as below

[vagrant@DevopsRoles ~]$ cat tr_command.txt 
 HuuPV, My website DevopsRoles.com and HuuPhan.com.SN:199x.
 Devops Roles.
 Devops Roles.
 Devops Roles.
 Devops Roles.
 Hello world :)

How to convert small letters into capital letters.

[vagrant@DevopsRoles ~]$ cat tr_command.txt | tr [:lower:] [:upper:]           
 HUUPV, MY WEBSITE DEVOPSROLES.COM AND HUUPHAN.COM.SN:199X.
 DEVOPS ROLES.
 DEVOPS ROLES.
 DEVOPS ROLES.
 DEVOPS ROLES.
 HELLO WORLD :)

The following command is used to convert each space of the text by a newline (\n)

[vagrant@DevopsRoles ~]$ cat tr_command.txt | tr [:space:] '\n'                                                                                                            
 HuuPV,
 My
 website
 DevopsRoles.com
 and
 HuuPhan.com.SN:199x.
 Devops
 Roles.
 Devops
 Roles.
 Devops
 Roles.
 Devops
 Roles.
 Hello
 world
 :)

uses –s option for searching and replacing any string from a text

[vagrant@DevopsRoles ~]$ cat tr_command.txt |  tr -s ' ' '\t'
 HuuPV,  My      website DevopsRoles.com and     HuuPhan.com.SN:199x.
 Devops  Roles.
 Devops  Roles.
 Devops  Roles.
 Devops  Roles.
 Hello   world   :)

Delete numbers

[vagrant@DevopsRoles ~]$ cat tr_command.txt | tr -d "[:digit:]"
 HuuPV, My website DevopsRoles.com and HuuPhan.com.SN:x.
 Devops Roles.
 Devops Roles.
 Devops Roles.
 Devops Roles.
 Hello world :)

Output the converted output to a file

[vagrant@DevopsRoles ~]$ cat tr_command.txt | tr "[:upper:]" "[:lower:]" > result_tr.txt 

Conclusion

tr command is a simple command in Linux. It is used to translate or delete characters. You can refer to the manual page for the tr command in Linux by typing man tr in the terminal for more information and additional options available on your specific Linux distribution.

Thank you for reading the DevopsRoles page!

,

About HuuPV

My name is Huu. I love technology and especially Devops Skill such as Docker, vagrant, git so forth. I likes open-sources. so I created DevopsRoles.com site to share the knowledge that I have learned. My Job: IT system administrator. Hobbies: summoners war game, gossip.
View all posts by HuuPV →

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.