Do not replace the second occurrence of pattern in a same word in the file with sed command

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











up vote
0
down vote

favorite












echo "abcdef12cade 12345678 abcdefba12345678" | sed -E "s/[0-9a-fA-F]8/00000000/g" 


the above outputs the following



00000000cade 00000000 0000000000000000


it is replacing the pattern for second occurrence in a same word.
I don't want to replace if there is a second occurrence.



expected ouput



00000000cade 00000000 abcdefba12345678






share|improve this question

























    up vote
    0
    down vote

    favorite












    echo "abcdef12cade 12345678 abcdefba12345678" | sed -E "s/[0-9a-fA-F]8/00000000/g" 


    the above outputs the following



    00000000cade 00000000 0000000000000000


    it is replacing the pattern for second occurrence in a same word.
    I don't want to replace if there is a second occurrence.



    expected ouput



    00000000cade 00000000 abcdefba12345678






    share|improve this question























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      echo "abcdef12cade 12345678 abcdefba12345678" | sed -E "s/[0-9a-fA-F]8/00000000/g" 


      the above outputs the following



      00000000cade 00000000 0000000000000000


      it is replacing the pattern for second occurrence in a same word.
      I don't want to replace if there is a second occurrence.



      expected ouput



      00000000cade 00000000 abcdefba12345678






      share|improve this question













      echo "abcdef12cade 12345678 abcdefba12345678" | sed -E "s/[0-9a-fA-F]8/00000000/g" 


      the above outputs the following



      00000000cade 00000000 0000000000000000


      it is replacing the pattern for second occurrence in a same word.
      I don't want to replace if there is a second occurrence.



      expected ouput



      00000000cade 00000000 abcdefba12345678








      share|improve this question












      share|improve this question




      share|improve this question








      edited Jun 21 at 15:47









      Jeff Schaller

      30.8k846104




      30.8k846104









      asked Jun 21 at 15:32









      hidayath

      1




      1




















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          3
          down vote













          If you only want to replace the first occurrence of a match, don't use the g suffix to the command:



          $ echo 'aa' | sed 's/a/b/g'
          bb
          $ echo 'aa' | sed 's/a/b/'
          ba


          The g option stands for 'global', which is explicitly telling sed to replace all matches and not just the first (which is the default behavior).






          share|improve this answer






























            up vote
            1
            down vote













            It seems like you're looking for words that are between 8 and 15 chars, and replace the first 8 hex digits:



            sed -E 's/<[[:xdigit:]]8([[:xdigit:]]0,7)>/000000001/g' <<END
            abcdef12cade 12345678 abcdefba12345678 12345 123456789
            END




            00000000cade 00000000 abcdefba12345678 12345 000000009


            Where, < and > are word boundaries, and [:xdigit:] matches a hex digit.






            share|improve this answer






























              up vote
              0
              down vote













              The g at the end make sed repeat the substitution as many times as possible on the line. You only want to do it twice.



              Let's do that with GNU awk:



              echo 'abcdef12cade 12345678 abcdefba12345678' |
              awk ' sub("[0-9a-fA-F]8", "00000000", $1)
              sub("[0-9a-fA-F]8", "00000000", $2)
              print '


              This performs the substitution on the two first whitespace-delimited fields only, and then prints the resulting line.






              share|improve this answer





















              • OP didn't want the second match replaced though?
                – DopeGhoti
                Jun 21 at 16:10










              • @DopeGhoti Their expected output is 00000000cade 00000000 abcdefba12345678, which is what this produces.
                – Kusalananda
                Jun 21 at 16:44










              • @DopeGhoti ... and I didn't read the title of the question until now, which leaves me more than slightly confused.
                – Kusalananda
                Jun 21 at 17:03










              • Yeah, the actual prose, title, and expected output presented are all somewhat contadictory.
                – DopeGhoti
                Jun 21 at 17:30










              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%2f451130%2fdo-not-replace-the-second-occurrence-of-pattern-in-a-same-word-in-the-file-with%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
              3
              down vote













              If you only want to replace the first occurrence of a match, don't use the g suffix to the command:



              $ echo 'aa' | sed 's/a/b/g'
              bb
              $ echo 'aa' | sed 's/a/b/'
              ba


              The g option stands for 'global', which is explicitly telling sed to replace all matches and not just the first (which is the default behavior).






              share|improve this answer



























                up vote
                3
                down vote













                If you only want to replace the first occurrence of a match, don't use the g suffix to the command:



                $ echo 'aa' | sed 's/a/b/g'
                bb
                $ echo 'aa' | sed 's/a/b/'
                ba


                The g option stands for 'global', which is explicitly telling sed to replace all matches and not just the first (which is the default behavior).






                share|improve this answer

























                  up vote
                  3
                  down vote










                  up vote
                  3
                  down vote









                  If you only want to replace the first occurrence of a match, don't use the g suffix to the command:



                  $ echo 'aa' | sed 's/a/b/g'
                  bb
                  $ echo 'aa' | sed 's/a/b/'
                  ba


                  The g option stands for 'global', which is explicitly telling sed to replace all matches and not just the first (which is the default behavior).






                  share|improve this answer















                  If you only want to replace the first occurrence of a match, don't use the g suffix to the command:



                  $ echo 'aa' | sed 's/a/b/g'
                  bb
                  $ echo 'aa' | sed 's/a/b/'
                  ba


                  The g option stands for 'global', which is explicitly telling sed to replace all matches and not just the first (which is the default behavior).







                  share|improve this answer















                  share|improve this answer



                  share|improve this answer








                  edited Jun 21 at 15:42


























                  answered Jun 21 at 15:38









                  DopeGhoti

                  39.7k54779




                  39.7k54779






















                      up vote
                      1
                      down vote













                      It seems like you're looking for words that are between 8 and 15 chars, and replace the first 8 hex digits:



                      sed -E 's/<[[:xdigit:]]8([[:xdigit:]]0,7)>/000000001/g' <<END
                      abcdef12cade 12345678 abcdefba12345678 12345 123456789
                      END




                      00000000cade 00000000 abcdefba12345678 12345 000000009


                      Where, < and > are word boundaries, and [:xdigit:] matches a hex digit.






                      share|improve this answer



























                        up vote
                        1
                        down vote













                        It seems like you're looking for words that are between 8 and 15 chars, and replace the first 8 hex digits:



                        sed -E 's/<[[:xdigit:]]8([[:xdigit:]]0,7)>/000000001/g' <<END
                        abcdef12cade 12345678 abcdefba12345678 12345 123456789
                        END




                        00000000cade 00000000 abcdefba12345678 12345 000000009


                        Where, < and > are word boundaries, and [:xdigit:] matches a hex digit.






                        share|improve this answer

























                          up vote
                          1
                          down vote










                          up vote
                          1
                          down vote









                          It seems like you're looking for words that are between 8 and 15 chars, and replace the first 8 hex digits:



                          sed -E 's/<[[:xdigit:]]8([[:xdigit:]]0,7)>/000000001/g' <<END
                          abcdef12cade 12345678 abcdefba12345678 12345 123456789
                          END




                          00000000cade 00000000 abcdefba12345678 12345 000000009


                          Where, < and > are word boundaries, and [:xdigit:] matches a hex digit.






                          share|improve this answer















                          It seems like you're looking for words that are between 8 and 15 chars, and replace the first 8 hex digits:



                          sed -E 's/<[[:xdigit:]]8([[:xdigit:]]0,7)>/000000001/g' <<END
                          abcdef12cade 12345678 abcdefba12345678 12345 123456789
                          END




                          00000000cade 00000000 abcdefba12345678 12345 000000009


                          Where, < and > are word boundaries, and [:xdigit:] matches a hex digit.







                          share|improve this answer















                          share|improve this answer



                          share|improve this answer








                          edited Jun 21 at 17:00


























                          answered Jun 21 at 16:03









                          glenn jackman

                          45.6k265100




                          45.6k265100




















                              up vote
                              0
                              down vote













                              The g at the end make sed repeat the substitution as many times as possible on the line. You only want to do it twice.



                              Let's do that with GNU awk:



                              echo 'abcdef12cade 12345678 abcdefba12345678' |
                              awk ' sub("[0-9a-fA-F]8", "00000000", $1)
                              sub("[0-9a-fA-F]8", "00000000", $2)
                              print '


                              This performs the substitution on the two first whitespace-delimited fields only, and then prints the resulting line.






                              share|improve this answer





















                              • OP didn't want the second match replaced though?
                                – DopeGhoti
                                Jun 21 at 16:10










                              • @DopeGhoti Their expected output is 00000000cade 00000000 abcdefba12345678, which is what this produces.
                                – Kusalananda
                                Jun 21 at 16:44










                              • @DopeGhoti ... and I didn't read the title of the question until now, which leaves me more than slightly confused.
                                – Kusalananda
                                Jun 21 at 17:03










                              • Yeah, the actual prose, title, and expected output presented are all somewhat contadictory.
                                – DopeGhoti
                                Jun 21 at 17:30














                              up vote
                              0
                              down vote













                              The g at the end make sed repeat the substitution as many times as possible on the line. You only want to do it twice.



                              Let's do that with GNU awk:



                              echo 'abcdef12cade 12345678 abcdefba12345678' |
                              awk ' sub("[0-9a-fA-F]8", "00000000", $1)
                              sub("[0-9a-fA-F]8", "00000000", $2)
                              print '


                              This performs the substitution on the two first whitespace-delimited fields only, and then prints the resulting line.






                              share|improve this answer





















                              • OP didn't want the second match replaced though?
                                – DopeGhoti
                                Jun 21 at 16:10










                              • @DopeGhoti Their expected output is 00000000cade 00000000 abcdefba12345678, which is what this produces.
                                – Kusalananda
                                Jun 21 at 16:44










                              • @DopeGhoti ... and I didn't read the title of the question until now, which leaves me more than slightly confused.
                                – Kusalananda
                                Jun 21 at 17:03










                              • Yeah, the actual prose, title, and expected output presented are all somewhat contadictory.
                                – DopeGhoti
                                Jun 21 at 17:30












                              up vote
                              0
                              down vote










                              up vote
                              0
                              down vote









                              The g at the end make sed repeat the substitution as many times as possible on the line. You only want to do it twice.



                              Let's do that with GNU awk:



                              echo 'abcdef12cade 12345678 abcdefba12345678' |
                              awk ' sub("[0-9a-fA-F]8", "00000000", $1)
                              sub("[0-9a-fA-F]8", "00000000", $2)
                              print '


                              This performs the substitution on the two first whitespace-delimited fields only, and then prints the resulting line.






                              share|improve this answer













                              The g at the end make sed repeat the substitution as many times as possible on the line. You only want to do it twice.



                              Let's do that with GNU awk:



                              echo 'abcdef12cade 12345678 abcdefba12345678' |
                              awk ' sub("[0-9a-fA-F]8", "00000000", $1)
                              sub("[0-9a-fA-F]8", "00000000", $2)
                              print '


                              This performs the substitution on the two first whitespace-delimited fields only, and then prints the resulting line.







                              share|improve this answer













                              share|improve this answer



                              share|improve this answer











                              answered Jun 21 at 15:52









                              Kusalananda

                              101k13199312




                              101k13199312











                              • OP didn't want the second match replaced though?
                                – DopeGhoti
                                Jun 21 at 16:10










                              • @DopeGhoti Their expected output is 00000000cade 00000000 abcdefba12345678, which is what this produces.
                                – Kusalananda
                                Jun 21 at 16:44










                              • @DopeGhoti ... and I didn't read the title of the question until now, which leaves me more than slightly confused.
                                – Kusalananda
                                Jun 21 at 17:03










                              • Yeah, the actual prose, title, and expected output presented are all somewhat contadictory.
                                – DopeGhoti
                                Jun 21 at 17:30
















                              • OP didn't want the second match replaced though?
                                – DopeGhoti
                                Jun 21 at 16:10










                              • @DopeGhoti Their expected output is 00000000cade 00000000 abcdefba12345678, which is what this produces.
                                – Kusalananda
                                Jun 21 at 16:44










                              • @DopeGhoti ... and I didn't read the title of the question until now, which leaves me more than slightly confused.
                                – Kusalananda
                                Jun 21 at 17:03










                              • Yeah, the actual prose, title, and expected output presented are all somewhat contadictory.
                                – DopeGhoti
                                Jun 21 at 17:30















                              OP didn't want the second match replaced though?
                              – DopeGhoti
                              Jun 21 at 16:10




                              OP didn't want the second match replaced though?
                              – DopeGhoti
                              Jun 21 at 16:10












                              @DopeGhoti Their expected output is 00000000cade 00000000 abcdefba12345678, which is what this produces.
                              – Kusalananda
                              Jun 21 at 16:44




                              @DopeGhoti Their expected output is 00000000cade 00000000 abcdefba12345678, which is what this produces.
                              – Kusalananda
                              Jun 21 at 16:44












                              @DopeGhoti ... and I didn't read the title of the question until now, which leaves me more than slightly confused.
                              – Kusalananda
                              Jun 21 at 17:03




                              @DopeGhoti ... and I didn't read the title of the question until now, which leaves me more than slightly confused.
                              – Kusalananda
                              Jun 21 at 17:03












                              Yeah, the actual prose, title, and expected output presented are all somewhat contadictory.
                              – DopeGhoti
                              Jun 21 at 17:30




                              Yeah, the actual prose, title, and expected output presented are all somewhat contadictory.
                              – DopeGhoti
                              Jun 21 at 17:30












                               

                              draft saved


                              draft discarded


























                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f451130%2fdo-not-replace-the-second-occurrence-of-pattern-in-a-same-word-in-the-file-with%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