{"id":7118,"date":"2018-10-19T14:05:40","date_gmt":"2018-10-19T11:05:40","guid":{"rendered":"https:\/\/www.howtoforge.com\/linux-tr-command\/"},"modified":"2018-10-19T14:05:40","modified_gmt":"2018-10-19T11:05:40","slug":"linux-tr-command-tutorial-for-beginners-with-examples","status":"publish","type":"post","link":"https:\/\/afaghhosting.net\/blog\/linux-tr-command-tutorial-for-beginners-with-examples\/","title":{"rendered":"Linux tr Command Tutorial for Beginners (with Examples)"},"content":{"rendered":"<p>Depending on the kind of work you do on the command line in Linux, you may want a utility that can act as a Swiss army knife of quick text editing. Gladly, there exists a tool dubbed tr, which qualifies for this role. In this tutorial, we will discuss the basics of tr using some easy to understand examples.<\/p>\n<p>But before we do that, it&#8217;s worth mentioning that all examples in this article have been tested on an Ubuntu 18.04 LTS machine.<\/p>\n<h2 id=\"linux-tr-command\">Linux tr command<\/h2>\n<p>Here&#8217;s how the tool&#8217;s man page explains it:<\/p>\n<pre>Translate, squeeze, and\/or delete characters from standard input, writing to standard output.<\/pre>\n<p>And following is its syntax:<\/p>\n<p class=\"command\">tr [OPTION]&#8230; SET1 [SET2]<\/p>\n<p>here&#8217;s what SET means:<\/p>\n<pre readability=\"21\">SETs are specified as strings of characters.\u00a0 Most represent themselves.\u00a0 Interpreted sequences are:<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \\NNN\u00a0\u00a0 character with octal value NNN (1 to 3 octal digits)<\/p><p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \\\\\u00a0\u00a0\u00a0\u00a0 backslash<\/p><p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \\a\u00a0\u00a0\u00a0\u00a0 audible BEL<\/p><p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \\b\u00a0\u00a0\u00a0\u00a0 backspace<\/p><p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \\f\u00a0\u00a0\u00a0\u00a0 form feed<\/p><p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \\n\u00a0\u00a0\u00a0\u00a0 new line<\/p><p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \\r\u00a0\u00a0\u00a0\u00a0 return<\/p><p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \\t\u00a0\u00a0\u00a0\u00a0 horizontal tab<\/p><p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \\v\u00a0\u00a0\u00a0\u00a0 vertical tab<\/p><\/pre>\n<p>Following are some Q&amp;A styled examples that should give you a better idea on how the tr command works.<\/p>\n<h2 id=\"q-how-to-convert-lower-case-to-upper-case-using-tr\">Q1. How to convert lower case to upper case using tr?<\/h2>\n<p>Suppose you want to convert the sentence &#8220;linux tutorial on howtoforge&#8221; to uppercase, then here&#8217;s how you can do this using tr.<\/p>\n<p class=\"command\">echo &#8216;linux tutorial on howtoforge&#8217; | tr &#8220;[:lower:]&#8221; &#8220;[:upper:]&#8221;<\/p>\n<p>The above command produced the following output on my system:<\/p>\n<pre>LINUX TUTORIAL ON HOWTOFORGE<\/pre>\n<h2 id=\"q-how-to-strip-extra-spaces-using-tr\">Q2. How to strip extra spaces using tr?<\/h2>\n<p>Suppose you have a line like: &#8220;HowtoForge\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 is an extremely \u00a0 \u00a0 \u00a0\u00a0 good resource for \u00a0 \u00a0\u00a0 Linux tutorials&#8221;. And the requirement is to strip extra spaces from this line.<\/p>\n<p>Here&#8217;s how you can use tr to do this:<\/p>\n<p class=\"command\">echo &#8216;HowtoForge\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 is an extremely\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 good resource for\u00a0\u00a0\u00a0\u00a0\u00a0 Linux tutorials&#8217; | tr -s &#8216;[:space:]&#8217;<\/p>\n<p>Here&#8217;s the output:<\/p>\n<pre>HowtoForge is an extremely good resource for Linux tutorials<\/pre>\n<p><a class=\"fancybox\" id=\"img-tr-space\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/tr-space.png\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/10\/linux-tr-command-tutorial-for-beginners-with-examples.png\" alt=\"\" title=\"\"><\/a><\/p>\n<h2 id=\"q-how-to-delete-text-using-tr\">Q3. How to delete text using tr?<\/h2>\n<p>Suppose you want to delete the hyphens from the following line: &#8220;HowtoForge &#8212; is &#8212; an &#8212; extremely &#8212; good &#8212; resource &#8212; for &#8212; Linux &#8212; tutorials.&#8221; Then here&#8217;s how you can do this using tr.<\/p>\n<p class=\"command\">echo &#8216;HowtoForge &#8212; is &#8212; an &#8212; extremely &#8212; good &#8212; resource &#8212; for &#8212; Linux &#8212; tutorials&#8217; | tr -d &#8216;-&#8216;<\/p>\n<p>Following is the output it produces:<\/p>\n<pre>HowtoForge\u00a0 is\u00a0 an\u00a0 extremely\u00a0 good\u00a0 resource\u00a0 for\u00a0 Linux\u00a0 tutorials<\/pre>\n<h2 id=\"q-how-to-replace-characters-using-tr\">Q4. How to replace characters using tr?<\/h2>\n<p>In the previous section, suppose the requirement was to replace hyphens with, let&#8217;s say, dots. Then here&#8217;s how you can do that using tr.<\/p>\n<p class=\"command\">echo &#8216;HowtoForge &#8212; is &#8212; an &#8212; extremely &#8212; good &#8212; resource &#8212; for &#8212; Linux &#8212; tutorials&#8217; | tr &#8216;-&#8216; &#8216;.&#8217;<\/p>\n<p>Following is the output it produced:<\/p>\n<pre>HowtoForge .. is .. an .. extremely .. good .. resource .. for .. Linux .. tutorials<\/pre>\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>So you can see the tr command is an extremely helpful tool when it comes to editing text. We have discussed some main options here, but the utility offers many other command line options as well. First try these, and once you&#8217;ve got a good idea about what we&#8217;ve discussed here, then you can learn more about tr by heading to its <a href=\"https:\/\/linux.die.net\/man\/1\/tr\" target=\"_blank\" rel=\"noopener noreferrer\">man page<\/a>.<\/p>\n<div class=\"authorbox\" readability=\"15\">\n<img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/10\/linux-tr-command-tutorial-for-beginners-with-examples.jpg\" alt=\"Himanshu Arora\" title=\"\"> <\/p>\n<p><strong>About Himanshu Arora<\/strong><\/p>\n<p>Himanshu Arora has been working on Linux since 2007. He carries professional experience in system level programming, networking protocols, and command line. In addition to HowtoForge, Himanshu&#8217;s work has also been featured in some of world&#8217;s other leading publications including Computerworld, IBM DeveloperWorks, and Linux Journal.<\/p>\n<\/div>\n<div>\n<p><b>Share this page:<\/b><\/p>\n<p>\n<a href=\"https:\/\/www.facebook.com\/sharer.php?u=https%3A%2F%2Fwww.howtoforge.com%2Flinux-tr-command%2F\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/10\/linux-tr-command-tutorial-for-beginners-with-examples-1.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/twitter.com\/intent\/tweet?url=https%3A%2F%2Fwww.howtoforge.com%2Flinux-tr-command%2F&amp;text=Linux+tr+Command+Tutorial+for+Beginners+%28with+Examples%29&amp;via=howtoforgecom&amp;related=howtoforgecom\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/10\/linux-tr-command-tutorial-for-beginners-with-examples-2.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/twitter.com\/howtoforgecom\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/10\/linux-tr-command-tutorial-for-beginners-with-examples-3.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/plus.google.com\/share?url=https%3A%2F%2Fwww.howtoforge.com%2Flinux-tr-command%2F\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/10\/linux-tr-command-tutorial-for-beginners-with-examples-4.png\" height=\"20\" alt=\"\" title=\"\"><\/a>\n<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Depending on the kind of work you do on the command line in Linux, you may want a utility that can act as a Swiss army knife of quick text editing. Gladly, there exists a tool dubbed tr, which qualifies for this role. In this tutorial, we will discuss the basics of tr using some [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[],"class_list":["post-7118","post","type-post","status-publish","format-standard","hentry","category-36"],"_links":{"self":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/7118","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/comments?post=7118"}],"version-history":[{"count":0,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/7118\/revisions"}],"wp:attachment":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/media?parent=7118"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/categories?post=7118"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/tags?post=7118"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}