Is the command tr capable of translating characters into longer strings?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
7
down vote

favorite
2












tr is a great tool that can be used to translate set of characters into another set of characters.



Commands such as sed , awk, perl can be used to translate set of characters into another set of strings, for example, it can translate < and > into &lt; and &gt;, but it seems unnecessary to use these commands for that purpose!



Is there any other command(s) that can accomplish this?










share|improve this question



























    up vote
    7
    down vote

    favorite
    2












    tr is a great tool that can be used to translate set of characters into another set of characters.



    Commands such as sed , awk, perl can be used to translate set of characters into another set of strings, for example, it can translate < and > into &lt; and &gt;, but it seems unnecessary to use these commands for that purpose!



    Is there any other command(s) that can accomplish this?










    share|improve this question

























      up vote
      7
      down vote

      favorite
      2









      up vote
      7
      down vote

      favorite
      2






      2





      tr is a great tool that can be used to translate set of characters into another set of characters.



      Commands such as sed , awk, perl can be used to translate set of characters into another set of strings, for example, it can translate < and > into &lt; and &gt;, but it seems unnecessary to use these commands for that purpose!



      Is there any other command(s) that can accomplish this?










      share|improve this question















      tr is a great tool that can be used to translate set of characters into another set of characters.



      Commands such as sed , awk, perl can be used to translate set of characters into another set of strings, for example, it can translate < and > into &lt; and &gt;, but it seems unnecessary to use these commands for that purpose!



      Is there any other command(s) that can accomplish this?







      shell string tr






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Sep 3 at 14:20









      Goro

      4,68452357




      4,68452357










      asked Jul 8 '11 at 14:13







      user4518



























          3 Answers
          3






          active

          oldest

          votes

















          up vote
          5
          down vote













          I don't know which shell you are using, but chances are tr is not a builtin. It's sure not for me in bash or zsh :) Try running type tr to see for yourself.



          In all likelyhood, sed is the right tool for the job. It's what it was made for. As appropriate perl and awk too.



          If you have very simple cases and if you are working with a string variable instead of a pipeline, you might get away with a real builtin such as using bash's string search and replace on a variable.



          string="Stuf with <tag> in it."
          escaped=$string//</&lt;
          escaped=$escaped//>/&gt;
          echo $escaped





          share|improve this answer






















          • I've fixed the terminology :)
            – user4518
            Jul 8 '11 at 14:24

















          up vote
          4
          down vote













          Replacing multiple strings with another set of multiple strings becomes messy if you want to use standard parameter handling. Usually, commands take a set of atomic parameters, and then a set of atomic arguments, and the sequence doesn't matter other than that arguments come after parameters (separated by -- if ambiguous). How would you construct the synopsis in this case?



          Here's a simple function which should do the job for simple replacements:



          $ trs() 
          local string=$1
          shift
          for replacement in "$@"
          do
          string="$(sed -e "s/$replacement/g" <<< "$string")"
          done
          printf "$string"

          $ trs '1 2 3' '1/foo' '2 3/bar baz'
          foo bar baz


          Alternatively you could work with pairs of parameters:



          $ trs() 
          local string=$1
          shift
          while [ $# -gt 0 ]
          do
          string="$(sed -e "s/$1/$2/g" <<< "$string")"
          shift 2
          done
          printf "$string"

          $ trs '1 2 3' 1 foo '2 3' 'bar baz'
          foo bar baz





          share|improve this answer




















          • These are nice functions. I hoped to find one that is readily available on a foreign system.
            – user4518
            Jul 8 '11 at 14:48










          • By the way, these wouldn't handle strings containing special strings like ? or &.
            – user4518
            Jul 8 '11 at 14:54






          • 1




            @Tim Nordenfur: If you only want to replace literals, you should escape it, yes. This approach allows for regular expressions, which of course is another can of worms.
            – l0b0
            Jul 8 '11 at 15:20

















          up vote
          3
          down vote













          sed can be scary, but simple character to string substitution with sed isn't really too bad.



          To substitute x with the string bar, do:



          something-that-outputs-something-on-stdout | sed -e 's/x/bar/g'



          if it's a file you're wanting to process, you can use:



          sed -e 's/x/bar/g' filename



          (The "g" at the end means "global" which means substitute all occurrences in the line and not just the first one)






          share|improve this answer


















          • 1




            I'm not scared of sed; I embrace it. The reason I'm asking is that using simpler tools seems to be generally encouraged, e.g. not sedding when you can cut.
            – user4518
            Jul 8 '11 at 14:44






          • 2




            how about not cating when you can just use sed
            – xenoterracide
            Jul 8 '11 at 15:44






          • 1




            @xenoterracide: Good point, edited answer.
            – LawrenceC
            Jul 8 '11 at 16:02










          • The only thing that I don't like here is that becomes quite difficult to replace a newline n with a string like ` ; ` using sed: sed: How can I replace a newline (n)? - Stack Overflow notes that plain s/// doesn't work, and you have to go into a "pattern space" syntax...
            – sdaau
            Jan 24 '15 at 3:12











          Your Answer







          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "106"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f16281%2fis-the-command-tr-capable-of-translating-characters-into-longer-strings%23new-answer', 'question_page');

          );

          Post as a guest





























          3 Answers
          3






          active

          oldest

          votes








          3 Answers
          3






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          5
          down vote













          I don't know which shell you are using, but chances are tr is not a builtin. It's sure not for me in bash or zsh :) Try running type tr to see for yourself.



          In all likelyhood, sed is the right tool for the job. It's what it was made for. As appropriate perl and awk too.



          If you have very simple cases and if you are working with a string variable instead of a pipeline, you might get away with a real builtin such as using bash's string search and replace on a variable.



          string="Stuf with <tag> in it."
          escaped=$string//</&lt;
          escaped=$escaped//>/&gt;
          echo $escaped





          share|improve this answer






















          • I've fixed the terminology :)
            – user4518
            Jul 8 '11 at 14:24














          up vote
          5
          down vote













          I don't know which shell you are using, but chances are tr is not a builtin. It's sure not for me in bash or zsh :) Try running type tr to see for yourself.



          In all likelyhood, sed is the right tool for the job. It's what it was made for. As appropriate perl and awk too.



          If you have very simple cases and if you are working with a string variable instead of a pipeline, you might get away with a real builtin such as using bash's string search and replace on a variable.



          string="Stuf with <tag> in it."
          escaped=$string//</&lt;
          escaped=$escaped//>/&gt;
          echo $escaped





          share|improve this answer






















          • I've fixed the terminology :)
            – user4518
            Jul 8 '11 at 14:24












          up vote
          5
          down vote










          up vote
          5
          down vote









          I don't know which shell you are using, but chances are tr is not a builtin. It's sure not for me in bash or zsh :) Try running type tr to see for yourself.



          In all likelyhood, sed is the right tool for the job. It's what it was made for. As appropriate perl and awk too.



          If you have very simple cases and if you are working with a string variable instead of a pipeline, you might get away with a real builtin such as using bash's string search and replace on a variable.



          string="Stuf with <tag> in it."
          escaped=$string//</&lt;
          escaped=$escaped//>/&gt;
          echo $escaped





          share|improve this answer














          I don't know which shell you are using, but chances are tr is not a builtin. It's sure not for me in bash or zsh :) Try running type tr to see for yourself.



          In all likelyhood, sed is the right tool for the job. It's what it was made for. As appropriate perl and awk too.



          If you have very simple cases and if you are working with a string variable instead of a pipeline, you might get away with a real builtin such as using bash's string search and replace on a variable.



          string="Stuf with <tag> in it."
          escaped=$string//</&lt;
          escaped=$escaped//>/&gt;
          echo $escaped






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jul 8 '11 at 14:33

























          answered Jul 8 '11 at 14:20









          Caleb

          49.2k9146185




          49.2k9146185











          • I've fixed the terminology :)
            – user4518
            Jul 8 '11 at 14:24
















          • I've fixed the terminology :)
            – user4518
            Jul 8 '11 at 14:24















          I've fixed the terminology :)
          – user4518
          Jul 8 '11 at 14:24




          I've fixed the terminology :)
          – user4518
          Jul 8 '11 at 14:24












          up vote
          4
          down vote













          Replacing multiple strings with another set of multiple strings becomes messy if you want to use standard parameter handling. Usually, commands take a set of atomic parameters, and then a set of atomic arguments, and the sequence doesn't matter other than that arguments come after parameters (separated by -- if ambiguous). How would you construct the synopsis in this case?



          Here's a simple function which should do the job for simple replacements:



          $ trs() 
          local string=$1
          shift
          for replacement in "$@"
          do
          string="$(sed -e "s/$replacement/g" <<< "$string")"
          done
          printf "$string"

          $ trs '1 2 3' '1/foo' '2 3/bar baz'
          foo bar baz


          Alternatively you could work with pairs of parameters:



          $ trs() 
          local string=$1
          shift
          while [ $# -gt 0 ]
          do
          string="$(sed -e "s/$1/$2/g" <<< "$string")"
          shift 2
          done
          printf "$string"

          $ trs '1 2 3' 1 foo '2 3' 'bar baz'
          foo bar baz





          share|improve this answer




















          • These are nice functions. I hoped to find one that is readily available on a foreign system.
            – user4518
            Jul 8 '11 at 14:48










          • By the way, these wouldn't handle strings containing special strings like ? or &.
            – user4518
            Jul 8 '11 at 14:54






          • 1




            @Tim Nordenfur: If you only want to replace literals, you should escape it, yes. This approach allows for regular expressions, which of course is another can of worms.
            – l0b0
            Jul 8 '11 at 15:20














          up vote
          4
          down vote













          Replacing multiple strings with another set of multiple strings becomes messy if you want to use standard parameter handling. Usually, commands take a set of atomic parameters, and then a set of atomic arguments, and the sequence doesn't matter other than that arguments come after parameters (separated by -- if ambiguous). How would you construct the synopsis in this case?



          Here's a simple function which should do the job for simple replacements:



          $ trs() 
          local string=$1
          shift
          for replacement in "$@"
          do
          string="$(sed -e "s/$replacement/g" <<< "$string")"
          done
          printf "$string"

          $ trs '1 2 3' '1/foo' '2 3/bar baz'
          foo bar baz


          Alternatively you could work with pairs of parameters:



          $ trs() 
          local string=$1
          shift
          while [ $# -gt 0 ]
          do
          string="$(sed -e "s/$1/$2/g" <<< "$string")"
          shift 2
          done
          printf "$string"

          $ trs '1 2 3' 1 foo '2 3' 'bar baz'
          foo bar baz





          share|improve this answer




















          • These are nice functions. I hoped to find one that is readily available on a foreign system.
            – user4518
            Jul 8 '11 at 14:48










          • By the way, these wouldn't handle strings containing special strings like ? or &.
            – user4518
            Jul 8 '11 at 14:54






          • 1




            @Tim Nordenfur: If you only want to replace literals, you should escape it, yes. This approach allows for regular expressions, which of course is another can of worms.
            – l0b0
            Jul 8 '11 at 15:20












          up vote
          4
          down vote










          up vote
          4
          down vote









          Replacing multiple strings with another set of multiple strings becomes messy if you want to use standard parameter handling. Usually, commands take a set of atomic parameters, and then a set of atomic arguments, and the sequence doesn't matter other than that arguments come after parameters (separated by -- if ambiguous). How would you construct the synopsis in this case?



          Here's a simple function which should do the job for simple replacements:



          $ trs() 
          local string=$1
          shift
          for replacement in "$@"
          do
          string="$(sed -e "s/$replacement/g" <<< "$string")"
          done
          printf "$string"

          $ trs '1 2 3' '1/foo' '2 3/bar baz'
          foo bar baz


          Alternatively you could work with pairs of parameters:



          $ trs() 
          local string=$1
          shift
          while [ $# -gt 0 ]
          do
          string="$(sed -e "s/$1/$2/g" <<< "$string")"
          shift 2
          done
          printf "$string"

          $ trs '1 2 3' 1 foo '2 3' 'bar baz'
          foo bar baz





          share|improve this answer












          Replacing multiple strings with another set of multiple strings becomes messy if you want to use standard parameter handling. Usually, commands take a set of atomic parameters, and then a set of atomic arguments, and the sequence doesn't matter other than that arguments come after parameters (separated by -- if ambiguous). How would you construct the synopsis in this case?



          Here's a simple function which should do the job for simple replacements:



          $ trs() 
          local string=$1
          shift
          for replacement in "$@"
          do
          string="$(sed -e "s/$replacement/g" <<< "$string")"
          done
          printf "$string"

          $ trs '1 2 3' '1/foo' '2 3/bar baz'
          foo bar baz


          Alternatively you could work with pairs of parameters:



          $ trs() 
          local string=$1
          shift
          while [ $# -gt 0 ]
          do
          string="$(sed -e "s/$1/$2/g" <<< "$string")"
          shift 2
          done
          printf "$string"

          $ trs '1 2 3' 1 foo '2 3' 'bar baz'
          foo bar baz






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jul 8 '11 at 14:41









          l0b0

          26.4k17106232




          26.4k17106232











          • These are nice functions. I hoped to find one that is readily available on a foreign system.
            – user4518
            Jul 8 '11 at 14:48










          • By the way, these wouldn't handle strings containing special strings like ? or &.
            – user4518
            Jul 8 '11 at 14:54






          • 1




            @Tim Nordenfur: If you only want to replace literals, you should escape it, yes. This approach allows for regular expressions, which of course is another can of worms.
            – l0b0
            Jul 8 '11 at 15:20
















          • These are nice functions. I hoped to find one that is readily available on a foreign system.
            – user4518
            Jul 8 '11 at 14:48










          • By the way, these wouldn't handle strings containing special strings like ? or &.
            – user4518
            Jul 8 '11 at 14:54






          • 1




            @Tim Nordenfur: If you only want to replace literals, you should escape it, yes. This approach allows for regular expressions, which of course is another can of worms.
            – l0b0
            Jul 8 '11 at 15:20















          These are nice functions. I hoped to find one that is readily available on a foreign system.
          – user4518
          Jul 8 '11 at 14:48




          These are nice functions. I hoped to find one that is readily available on a foreign system.
          – user4518
          Jul 8 '11 at 14:48












          By the way, these wouldn't handle strings containing special strings like ? or &.
          – user4518
          Jul 8 '11 at 14:54




          By the way, these wouldn't handle strings containing special strings like ? or &.
          – user4518
          Jul 8 '11 at 14:54




          1




          1




          @Tim Nordenfur: If you only want to replace literals, you should escape it, yes. This approach allows for regular expressions, which of course is another can of worms.
          – l0b0
          Jul 8 '11 at 15:20




          @Tim Nordenfur: If you only want to replace literals, you should escape it, yes. This approach allows for regular expressions, which of course is another can of worms.
          – l0b0
          Jul 8 '11 at 15:20










          up vote
          3
          down vote













          sed can be scary, but simple character to string substitution with sed isn't really too bad.



          To substitute x with the string bar, do:



          something-that-outputs-something-on-stdout | sed -e 's/x/bar/g'



          if it's a file you're wanting to process, you can use:



          sed -e 's/x/bar/g' filename



          (The "g" at the end means "global" which means substitute all occurrences in the line and not just the first one)






          share|improve this answer


















          • 1




            I'm not scared of sed; I embrace it. The reason I'm asking is that using simpler tools seems to be generally encouraged, e.g. not sedding when you can cut.
            – user4518
            Jul 8 '11 at 14:44






          • 2




            how about not cating when you can just use sed
            – xenoterracide
            Jul 8 '11 at 15:44






          • 1




            @xenoterracide: Good point, edited answer.
            – LawrenceC
            Jul 8 '11 at 16:02










          • The only thing that I don't like here is that becomes quite difficult to replace a newline n with a string like ` ; ` using sed: sed: How can I replace a newline (n)? - Stack Overflow notes that plain s/// doesn't work, and you have to go into a "pattern space" syntax...
            – sdaau
            Jan 24 '15 at 3:12















          up vote
          3
          down vote













          sed can be scary, but simple character to string substitution with sed isn't really too bad.



          To substitute x with the string bar, do:



          something-that-outputs-something-on-stdout | sed -e 's/x/bar/g'



          if it's a file you're wanting to process, you can use:



          sed -e 's/x/bar/g' filename



          (The "g" at the end means "global" which means substitute all occurrences in the line and not just the first one)






          share|improve this answer


















          • 1




            I'm not scared of sed; I embrace it. The reason I'm asking is that using simpler tools seems to be generally encouraged, e.g. not sedding when you can cut.
            – user4518
            Jul 8 '11 at 14:44






          • 2




            how about not cating when you can just use sed
            – xenoterracide
            Jul 8 '11 at 15:44






          • 1




            @xenoterracide: Good point, edited answer.
            – LawrenceC
            Jul 8 '11 at 16:02










          • The only thing that I don't like here is that becomes quite difficult to replace a newline n with a string like ` ; ` using sed: sed: How can I replace a newline (n)? - Stack Overflow notes that plain s/// doesn't work, and you have to go into a "pattern space" syntax...
            – sdaau
            Jan 24 '15 at 3:12













          up vote
          3
          down vote










          up vote
          3
          down vote









          sed can be scary, but simple character to string substitution with sed isn't really too bad.



          To substitute x with the string bar, do:



          something-that-outputs-something-on-stdout | sed -e 's/x/bar/g'



          if it's a file you're wanting to process, you can use:



          sed -e 's/x/bar/g' filename



          (The "g" at the end means "global" which means substitute all occurrences in the line and not just the first one)






          share|improve this answer














          sed can be scary, but simple character to string substitution with sed isn't really too bad.



          To substitute x with the string bar, do:



          something-that-outputs-something-on-stdout | sed -e 's/x/bar/g'



          if it's a file you're wanting to process, you can use:



          sed -e 's/x/bar/g' filename



          (The "g" at the end means "global" which means substitute all occurrences in the line and not just the first one)







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Sep 3 at 13:53









          Community♦

          1




          1










          answered Jul 8 '11 at 14:41









          LawrenceC

          8,31722240




          8,31722240







          • 1




            I'm not scared of sed; I embrace it. The reason I'm asking is that using simpler tools seems to be generally encouraged, e.g. not sedding when you can cut.
            – user4518
            Jul 8 '11 at 14:44






          • 2




            how about not cating when you can just use sed
            – xenoterracide
            Jul 8 '11 at 15:44






          • 1




            @xenoterracide: Good point, edited answer.
            – LawrenceC
            Jul 8 '11 at 16:02










          • The only thing that I don't like here is that becomes quite difficult to replace a newline n with a string like ` ; ` using sed: sed: How can I replace a newline (n)? - Stack Overflow notes that plain s/// doesn't work, and you have to go into a "pattern space" syntax...
            – sdaau
            Jan 24 '15 at 3:12













          • 1




            I'm not scared of sed; I embrace it. The reason I'm asking is that using simpler tools seems to be generally encouraged, e.g. not sedding when you can cut.
            – user4518
            Jul 8 '11 at 14:44






          • 2




            how about not cating when you can just use sed
            – xenoterracide
            Jul 8 '11 at 15:44






          • 1




            @xenoterracide: Good point, edited answer.
            – LawrenceC
            Jul 8 '11 at 16:02










          • The only thing that I don't like here is that becomes quite difficult to replace a newline n with a string like ` ; ` using sed: sed: How can I replace a newline (n)? - Stack Overflow notes that plain s/// doesn't work, and you have to go into a "pattern space" syntax...
            – sdaau
            Jan 24 '15 at 3:12








          1




          1




          I'm not scared of sed; I embrace it. The reason I'm asking is that using simpler tools seems to be generally encouraged, e.g. not sedding when you can cut.
          – user4518
          Jul 8 '11 at 14:44




          I'm not scared of sed; I embrace it. The reason I'm asking is that using simpler tools seems to be generally encouraged, e.g. not sedding when you can cut.
          – user4518
          Jul 8 '11 at 14:44




          2




          2




          how about not cating when you can just use sed
          – xenoterracide
          Jul 8 '11 at 15:44




          how about not cating when you can just use sed
          – xenoterracide
          Jul 8 '11 at 15:44




          1




          1




          @xenoterracide: Good point, edited answer.
          – LawrenceC
          Jul 8 '11 at 16:02




          @xenoterracide: Good point, edited answer.
          – LawrenceC
          Jul 8 '11 at 16:02












          The only thing that I don't like here is that becomes quite difficult to replace a newline n with a string like ` ; ` using sed: sed: How can I replace a newline (n)? - Stack Overflow notes that plain s/// doesn't work, and you have to go into a "pattern space" syntax...
          – sdaau
          Jan 24 '15 at 3:12





          The only thing that I don't like here is that becomes quite difficult to replace a newline n with a string like ` ; ` using sed: sed: How can I replace a newline (n)? - Stack Overflow notes that plain s/// doesn't work, and you have to go into a "pattern space" syntax...
          – sdaau
          Jan 24 '15 at 3:12


















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f16281%2fis-the-command-tr-capable-of-translating-characters-into-longer-strings%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          How to check contact read email or not when send email to Individual?

          Bahrain

          Postfix configuration issue with fips on centos 7; mailgun relay