Switch characters in a string using sed

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












1















How would you switch characters in a string using sed?
Say we had a number 12345678 and I wanted to switch the 2nd and 4th digit with each other (i.e. number = 14325678).










share|improve this question
























  • sed modifies lines, not strings. Where on the line is this string that you want to modify? Is the number the only thing on each line?

    – Kusalananda
    Feb 11 at 6:50
















1















How would you switch characters in a string using sed?
Say we had a number 12345678 and I wanted to switch the 2nd and 4th digit with each other (i.e. number = 14325678).










share|improve this question
























  • sed modifies lines, not strings. Where on the line is this string that you want to modify? Is the number the only thing on each line?

    – Kusalananda
    Feb 11 at 6:50














1












1








1








How would you switch characters in a string using sed?
Say we had a number 12345678 and I wanted to switch the 2nd and 4th digit with each other (i.e. number = 14325678).










share|improve this question
















How would you switch characters in a string using sed?
Say we had a number 12345678 and I wanted to switch the 2nd and 4th digit with each other (i.e. number = 14325678).







text-processing sed






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 11 at 3:47









αғsнιη

17k102966




17k102966










asked Feb 10 at 23:51









JKFJKF

61




61












  • sed modifies lines, not strings. Where on the line is this string that you want to modify? Is the number the only thing on each line?

    – Kusalananda
    Feb 11 at 6:50


















  • sed modifies lines, not strings. Where on the line is this string that you want to modify? Is the number the only thing on each line?

    – Kusalananda
    Feb 11 at 6:50

















sed modifies lines, not strings. Where on the line is this string that you want to modify? Is the number the only thing on each line?

– Kusalananda
Feb 11 at 6:50






sed modifies lines, not strings. Where on the line is this string that you want to modify? Is the number the only thing on each line?

– Kusalananda
Feb 11 at 6:50











2 Answers
2






active

oldest

votes


















2














Group parts of the expression with parenthesis and later recall these groups in different order with n where n is a group index. For example



$ echo 12345678 | sed -E 's/(.)(.)(.)(.)/1432/'
14325678





share|improve this answer






























    1














    You can do it multiple ways :



     echo 12345678 |
    sed -e '
    s/./&n/4
    s/./n&/2
    s/n(.)(.*)(.)n/321/
    '
    14325678

    perl -lne 'print+(unpack "AAAAA*")[0,3,2,1,4]'

    perl -F -lane '@F[1,3] = @F[3,1];print @F'

    awk -F "" 't=$2;$2=$4;$4=t1' OFS=


    With awk, split on empty string so each character becomes a field, which are then shuffled and by way of Ofs var they get taken to stdout.



    With Perl, we unpack the string using an ascii template, then rearrange the order and finally print to stdout. The default Ofs is null.



    With sed, first we mark the chars needed to be flipped. Then in the last step we exchange them with the help of markers. This method scales with any two char positions you may want to exchange.



    HTH.






    share|improve this answer
























      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',
      autoActivateHeartbeat: false,
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      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%2f499836%2fswitch-characters-in-a-string-using-sed%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      Group parts of the expression with parenthesis and later recall these groups in different order with n where n is a group index. For example



      $ echo 12345678 | sed -E 's/(.)(.)(.)(.)/1432/'
      14325678





      share|improve this answer



























        2














        Group parts of the expression with parenthesis and later recall these groups in different order with n where n is a group index. For example



        $ echo 12345678 | sed -E 's/(.)(.)(.)(.)/1432/'
        14325678





        share|improve this answer

























          2












          2








          2







          Group parts of the expression with parenthesis and later recall these groups in different order with n where n is a group index. For example



          $ echo 12345678 | sed -E 's/(.)(.)(.)(.)/1432/'
          14325678





          share|improve this answer













          Group parts of the expression with parenthesis and later recall these groups in different order with n where n is a group index. For example



          $ echo 12345678 | sed -E 's/(.)(.)(.)(.)/1432/'
          14325678






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Feb 11 at 0:02









          jimmijjimmij

          32.1k874108




          32.1k874108























              1














              You can do it multiple ways :



               echo 12345678 |
              sed -e '
              s/./&n/4
              s/./n&/2
              s/n(.)(.*)(.)n/321/
              '
              14325678

              perl -lne 'print+(unpack "AAAAA*")[0,3,2,1,4]'

              perl -F -lane '@F[1,3] = @F[3,1];print @F'

              awk -F "" 't=$2;$2=$4;$4=t1' OFS=


              With awk, split on empty string so each character becomes a field, which are then shuffled and by way of Ofs var they get taken to stdout.



              With Perl, we unpack the string using an ascii template, then rearrange the order and finally print to stdout. The default Ofs is null.



              With sed, first we mark the chars needed to be flipped. Then in the last step we exchange them with the help of markers. This method scales with any two char positions you may want to exchange.



              HTH.






              share|improve this answer





























                1














                You can do it multiple ways :



                 echo 12345678 |
                sed -e '
                s/./&n/4
                s/./n&/2
                s/n(.)(.*)(.)n/321/
                '
                14325678

                perl -lne 'print+(unpack "AAAAA*")[0,3,2,1,4]'

                perl -F -lane '@F[1,3] = @F[3,1];print @F'

                awk -F "" 't=$2;$2=$4;$4=t1' OFS=


                With awk, split on empty string so each character becomes a field, which are then shuffled and by way of Ofs var they get taken to stdout.



                With Perl, we unpack the string using an ascii template, then rearrange the order and finally print to stdout. The default Ofs is null.



                With sed, first we mark the chars needed to be flipped. Then in the last step we exchange them with the help of markers. This method scales with any two char positions you may want to exchange.



                HTH.






                share|improve this answer



























                  1












                  1








                  1







                  You can do it multiple ways :



                   echo 12345678 |
                  sed -e '
                  s/./&n/4
                  s/./n&/2
                  s/n(.)(.*)(.)n/321/
                  '
                  14325678

                  perl -lne 'print+(unpack "AAAAA*")[0,3,2,1,4]'

                  perl -F -lane '@F[1,3] = @F[3,1];print @F'

                  awk -F "" 't=$2;$2=$4;$4=t1' OFS=


                  With awk, split on empty string so each character becomes a field, which are then shuffled and by way of Ofs var they get taken to stdout.



                  With Perl, we unpack the string using an ascii template, then rearrange the order and finally print to stdout. The default Ofs is null.



                  With sed, first we mark the chars needed to be flipped. Then in the last step we exchange them with the help of markers. This method scales with any two char positions you may want to exchange.



                  HTH.






                  share|improve this answer















                  You can do it multiple ways :



                   echo 12345678 |
                  sed -e '
                  s/./&n/4
                  s/./n&/2
                  s/n(.)(.*)(.)n/321/
                  '
                  14325678

                  perl -lne 'print+(unpack "AAAAA*")[0,3,2,1,4]'

                  perl -F -lane '@F[1,3] = @F[3,1];print @F'

                  awk -F "" 't=$2;$2=$4;$4=t1' OFS=


                  With awk, split on empty string so each character becomes a field, which are then shuffled and by way of Ofs var they get taken to stdout.



                  With Perl, we unpack the string using an ascii template, then rearrange the order and finally print to stdout. The default Ofs is null.



                  With sed, first we mark the chars needed to be flipped. Then in the last step we exchange them with the help of markers. This method scales with any two char positions you may want to exchange.



                  HTH.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Feb 11 at 5:07

























                  answered Feb 11 at 4:57









                  Rakesh SharmaRakesh Sharma

                  344114




                  344114



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Unix & Linux Stack Exchange!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f499836%2fswitch-characters-in-a-string-using-sed%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown






                      Popular posts from this blog

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

                      Displaying single band from multi-band raster using QGIS

                      How many registers does an x86_64 CPU actually have?