{"id":3043,"date":"2018-11-11T08:12:48","date_gmt":"2018-11-11T08:12:48","guid":{"rendered":"https:\/\/www.appservgrid.com\/paw92\/?p=3043"},"modified":"2018-11-12T02:34:16","modified_gmt":"2018-11-12T02:34:16","slug":"bash-tr-command-linux-hint","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2018\/11\/11\/bash-tr-command-linux-hint\/","title":{"rendered":"Bash tr command | Linux Hint"},"content":{"rendered":"<p>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<\/p>\n<p>tr command are shown in this tutorial.<\/p>\n<p>Syntax:<\/p>\n<p>tr [option] stringValue1 [stringValue2]<\/p>\n<p>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.<\/p>\n<p>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].<\/p>\n<p>tr command can be used in the following way to convert any string from uppercase to lowercase.<\/p>\n<p>tr [:upper:] [:lower:]<\/p>\n<p>You can use tr command in the following way also to convert any string from lowercase to uppercase.<\/p>\n<p>tr a-z A-Z<\/p>\n<p>Run the following command to convert every small letter of the string,\u2019linuxhint\u2019 into the capital letter.<\/p>\n<p>$ echo linuxhint | tr [:lower:] [:upper:]<\/p>\n<p>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.<\/p>\n<ol>\n<li>Monitor<\/li>\n<li>Keyboard<\/li>\n<li>Mouse<\/li>\n<li>Scanner<\/li>\n<li>HDD<\/li>\n<\/ol>\n<p>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.<\/p>\n<p>$ cat items.txt<br \/>\n$ tr a-z A-Z &lt; items.txt<\/p>\n<p>You can run the following command to store the output of the tr command into another file named \u2018output.txt\u2019.<\/p>\n<p>$ tr [:upper:] [:lower:] &lt; items.txt &gt; output.txt<br \/>\n$ cat output.txt<img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/linuxhint.com\/image\/gif;base64,R0lGODdhAQABAPAAAP\/wAAACwAAAAAAQABAEACAkQBADs=\" alt=\"\" width=\"731\" height=\"156\" \/><\/p>\n<h2>Example-2: Translate character<\/h2>\n<p>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, \u201cWelcome to Linuxhint\u201d by newline (n).<\/p>\n<p>$ echo &#8220;Welcome To Linuxhint&#8221; | tr [:space:] &#8216;n&#8217;<img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/linuxhint.com\/image\/gif;base64,R0lGODdhAQABAPAAAP\/wAAACwAAAAAAQABAEACAkQBADs=\" alt=\"\" width=\"728\" height=\"106\" \/><\/p>\n<h3>Example-3: Using \u2013c option<\/h3>\n<p>tr command can be used with -c option to replace those characters with the second character that don\u2019t match with the first character value. In the following example, tr command is used to search those characters in the string \u2018bash\u2019 that don\u2019t match with the character \u2018b\u2019 and replace them by \u2018a\u2019. The output is \u2018baaaa\u2019. Four characters are converted here. These are ,\u2019a\u2019,\u2019s\u2019,\u2019h\u2019 and \u2018n\u2019.<\/p>\n<p>$ echo &#8220;bash&#8221; | tr -c &#8216;b&#8217; &#8216;a&#8217;<img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/linuxhint.com\/image\/gif;base64,R0lGODdhAQABAPAAAP\/wAAACwAAAAAAQABAEACAkQBADs=\" alt=\"\" width=\"624\" height=\"49\" \/><\/p>\n<h3>Example-4: Using \u2013s option<\/h3>\n<p>tr command uses \u2013s option for search and replace any string from a text. In the following example, space (\u2018 \u2018) is replaced by tab (\u2018t\u2019).<\/p>\n<p>$ echo &#8220;BASH Programming&#8221; | tr -s &#8216; &#8216; &#8216;t&#8217;<img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/linuxhint.com\/image\/gif;base64,R0lGODdhAQABAPAAAP\/wAAACwAAAAAAQABAEACAkQBADs=\" alt=\"\" width=\"734\" height=\"61\" \/><\/p>\n<p>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 \u2013c option, tr command will search and replace each capital letter by newline (\u2018n\u2019) of the file, items.txt and store the output of the command in the file, output.txt.<\/p>\n<p>$ cat items.txt<br \/>\n$ tr -cs [a-z] &#8220;n&#8221; &lt; items.txt &gt; output.txt<br \/>\n$ cat output.txt<img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/linuxhint.com\/image\/gif;base64,R0lGODdhAQABAPAAAP\/wAAACwAAAAAAQABAEACAkQBADs=\" alt=\"\" width=\"737\" height=\"267\" \/><\/p>\n<h3>Example-5: Using \u2013d option<\/h3>\n<p>-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 \u2018P\u2019, \u2018y\u2019 and \u2018t\u2019 in the string \u201cPython is a Programming language\u201d and delete those characters.<\/p>\n<p>$ echo &#8220;Python is a Programming language&#8221; | tr -d &#8216;Pyt&#8217;<img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/linuxhint.com\/image\/gif;base64,R0lGODdhAQABAPAAAP\/wAAACwAAAAAAQABAEACAkQBADs=\" alt=\"\" width=\"740\" height=\"70\" \/><\/p>\n<p>-c option can be used with \u2013d option in the tr command to complement the search like precious \u2013cs command. In the following example, tr command with \u2013cd will search all non-digit characters from the string, \u201cPhone No: 985634854\u201d and delete them.<\/p>\n<p>$ echo &#8220;Phone No: 985634854&#8221; | tr -cd &#8216;0-9&#8217;<img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/linuxhint.com\/image\/gif;base64,R0lGODdhAQABAPAAAP\/wAAACwAAAAAAQABAEACAkQBADs=\" alt=\"\" width=\"732\" height=\"57\" \/><\/p>\n<p>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.<\/p>\n<p>$ tr -cd &#8220;[:print:]&#8221; &lt; items.txt<\/p>\n<h4>Conclusion<\/h4>\n<p>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.<\/p>\n<p><a href=\"https:\/\/linuxhint.com\/bash_tr_command\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2018\/11\/11\/bash-tr-command-linux-hint\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Bash tr command | Linux Hint&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3043","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/3043","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=3043"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/3043\/revisions"}],"predecessor-version":[{"id":3258,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/3043\/revisions\/3258"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=3043"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=3043"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=3043"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}