sed: delete all occurrences of a string except the first one

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











up vote
13
down vote

favorite












I have a logfile with timestamps in it. Occasionally there are multiple timestamps in one line. Now I would like to remove all of the timestamps from a line but keep the first one.



I can do s/pattern//2 but that only removes the second occurrence and sed doesn't allow something like s/pattern//2-.



Any suggestions?










share|improve this question























  • I should've told that it's the sed of busybox. Sorry for that.
    – Folkert van Heusden
    Aug 8 '11 at 12:08














up vote
13
down vote

favorite












I have a logfile with timestamps in it. Occasionally there are multiple timestamps in one line. Now I would like to remove all of the timestamps from a line but keep the first one.



I can do s/pattern//2 but that only removes the second occurrence and sed doesn't allow something like s/pattern//2-.



Any suggestions?










share|improve this question























  • I should've told that it's the sed of busybox. Sorry for that.
    – Folkert van Heusden
    Aug 8 '11 at 12:08












up vote
13
down vote

favorite









up vote
13
down vote

favorite











I have a logfile with timestamps in it. Occasionally there are multiple timestamps in one line. Now I would like to remove all of the timestamps from a line but keep the first one.



I can do s/pattern//2 but that only removes the second occurrence and sed doesn't allow something like s/pattern//2-.



Any suggestions?










share|improve this question















I have a logfile with timestamps in it. Occasionally there are multiple timestamps in one line. Now I would like to remove all of the timestamps from a line but keep the first one.



I can do s/pattern//2 but that only removes the second occurrence and sed doesn't allow something like s/pattern//2-.



Any suggestions?







sed






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 8 '11 at 11:05









Caleb

49.2k9146185




49.2k9146185










asked Aug 8 '11 at 6:58









Folkert van Heusden

597515




597515











  • I should've told that it's the sed of busybox. Sorry for that.
    – Folkert van Heusden
    Aug 8 '11 at 12:08
















  • I should've told that it's the sed of busybox. Sorry for that.
    – Folkert van Heusden
    Aug 8 '11 at 12:08















I should've told that it's the sed of busybox. Sorry for that.
– Folkert van Heusden
Aug 8 '11 at 12:08




I should've told that it's the sed of busybox. Sorry for that.
– Folkert van Heusden
Aug 8 '11 at 12:08










4 Answers
4






active

oldest

votes

















up vote
4
down vote



accepted










With GNU sed:



sed 's/pattern//2g'


The 2 specifies that the second pattern and all the restg should remove. So this will keep the first one.






share|improve this answer


















  • 2




    That will only work with GNU sed.
    – Stéphane Chazelas
    Dec 18 '14 at 8:38






  • 1




    Which Cygwin has (a port of, apparently) and MacOS does not. Achh! This solution is so much more elegant.
    – r_alex_hall
    Jun 2 '17 at 4:03

















up vote
7
down vote













This should work (replace _ by something else should it clash with your logs):



sed -e 's/pattern/_&/1' -e 's/([^_])pattern//g' -e 's/_(pattern)/1/'





share|improve this answer
















  • 1




    if you're ever wanting a unique delimiter, use n.
    – mikeserv
    Dec 19 '14 at 13:35

















up vote
5
down vote













sed -e ':begin;s/pattern//2;t begin'


or without the sed goto:



sed -e 's/(pattern)/1n/;h;s/.*n//;s/pattern//g;H;g;s/n.*n//'


The generic solutions to remove from the nth (3 for example) position are:



sed -e ':begin;s/pattern//4;t begin'
sed -e 's/(pattern)/1n/;h;s/.*n//3;s/pattern//g;H;g;s/n.*n//'





share|improve this answer





























    up vote
    1
    down vote













    A slight variation on @jillagre's answer (modified for robustness) could look like:



    sed 's/p(attern)/pn1/;s///g;s/n//'


    ...but in some seds you may need to replace the n in the right-hand side of the first s///ubstitution statement with a literal newline character.






    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',
      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%2f18303%2fsed-delete-all-occurrences-of-a-string-except-the-first-one%23new-answer', 'question_page');

      );

      Post as a guest






























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      4
      down vote



      accepted










      With GNU sed:



      sed 's/pattern//2g'


      The 2 specifies that the second pattern and all the restg should remove. So this will keep the first one.






      share|improve this answer


















      • 2




        That will only work with GNU sed.
        – Stéphane Chazelas
        Dec 18 '14 at 8:38






      • 1




        Which Cygwin has (a port of, apparently) and MacOS does not. Achh! This solution is so much more elegant.
        – r_alex_hall
        Jun 2 '17 at 4:03














      up vote
      4
      down vote



      accepted










      With GNU sed:



      sed 's/pattern//2g'


      The 2 specifies that the second pattern and all the restg should remove. So this will keep the first one.






      share|improve this answer


















      • 2




        That will only work with GNU sed.
        – Stéphane Chazelas
        Dec 18 '14 at 8:38






      • 1




        Which Cygwin has (a port of, apparently) and MacOS does not. Achh! This solution is so much more elegant.
        – r_alex_hall
        Jun 2 '17 at 4:03












      up vote
      4
      down vote



      accepted







      up vote
      4
      down vote



      accepted






      With GNU sed:



      sed 's/pattern//2g'


      The 2 specifies that the second pattern and all the restg should remove. So this will keep the first one.






      share|improve this answer














      With GNU sed:



      sed 's/pattern//2g'


      The 2 specifies that the second pattern and all the restg should remove. So this will keep the first one.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Oct 5 '16 at 16:14









      terdon♦

      123k28232406




      123k28232406










      answered Dec 18 '14 at 8:24









      αғsнιη

      16k92563




      16k92563







      • 2




        That will only work with GNU sed.
        – Stéphane Chazelas
        Dec 18 '14 at 8:38






      • 1




        Which Cygwin has (a port of, apparently) and MacOS does not. Achh! This solution is so much more elegant.
        – r_alex_hall
        Jun 2 '17 at 4:03












      • 2




        That will only work with GNU sed.
        – Stéphane Chazelas
        Dec 18 '14 at 8:38






      • 1




        Which Cygwin has (a port of, apparently) and MacOS does not. Achh! This solution is so much more elegant.
        – r_alex_hall
        Jun 2 '17 at 4:03







      2




      2




      That will only work with GNU sed.
      – Stéphane Chazelas
      Dec 18 '14 at 8:38




      That will only work with GNU sed.
      – Stéphane Chazelas
      Dec 18 '14 at 8:38




      1




      1




      Which Cygwin has (a port of, apparently) and MacOS does not. Achh! This solution is so much more elegant.
      – r_alex_hall
      Jun 2 '17 at 4:03




      Which Cygwin has (a port of, apparently) and MacOS does not. Achh! This solution is so much more elegant.
      – r_alex_hall
      Jun 2 '17 at 4:03












      up vote
      7
      down vote













      This should work (replace _ by something else should it clash with your logs):



      sed -e 's/pattern/_&/1' -e 's/([^_])pattern//g' -e 's/_(pattern)/1/'





      share|improve this answer
















      • 1




        if you're ever wanting a unique delimiter, use n.
        – mikeserv
        Dec 19 '14 at 13:35














      up vote
      7
      down vote













      This should work (replace _ by something else should it clash with your logs):



      sed -e 's/pattern/_&/1' -e 's/([^_])pattern//g' -e 's/_(pattern)/1/'





      share|improve this answer
















      • 1




        if you're ever wanting a unique delimiter, use n.
        – mikeserv
        Dec 19 '14 at 13:35












      up vote
      7
      down vote










      up vote
      7
      down vote









      This should work (replace _ by something else should it clash with your logs):



      sed -e 's/pattern/_&/1' -e 's/([^_])pattern//g' -e 's/_(pattern)/1/'





      share|improve this answer












      This should work (replace _ by something else should it clash with your logs):



      sed -e 's/pattern/_&/1' -e 's/([^_])pattern//g' -e 's/_(pattern)/1/'






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Aug 8 '11 at 11:41









      jlliagre

      45.2k578125




      45.2k578125







      • 1




        if you're ever wanting a unique delimiter, use n.
        – mikeserv
        Dec 19 '14 at 13:35












      • 1




        if you're ever wanting a unique delimiter, use n.
        – mikeserv
        Dec 19 '14 at 13:35







      1




      1




      if you're ever wanting a unique delimiter, use n.
      – mikeserv
      Dec 19 '14 at 13:35




      if you're ever wanting a unique delimiter, use n.
      – mikeserv
      Dec 19 '14 at 13:35










      up vote
      5
      down vote













      sed -e ':begin;s/pattern//2;t begin'


      or without the sed goto:



      sed -e 's/(pattern)/1n/;h;s/.*n//;s/pattern//g;H;g;s/n.*n//'


      The generic solutions to remove from the nth (3 for example) position are:



      sed -e ':begin;s/pattern//4;t begin'
      sed -e 's/(pattern)/1n/;h;s/.*n//3;s/pattern//g;H;g;s/n.*n//'





      share|improve this answer


























        up vote
        5
        down vote













        sed -e ':begin;s/pattern//2;t begin'


        or without the sed goto:



        sed -e 's/(pattern)/1n/;h;s/.*n//;s/pattern//g;H;g;s/n.*n//'


        The generic solutions to remove from the nth (3 for example) position are:



        sed -e ':begin;s/pattern//4;t begin'
        sed -e 's/(pattern)/1n/;h;s/.*n//3;s/pattern//g;H;g;s/n.*n//'





        share|improve this answer
























          up vote
          5
          down vote










          up vote
          5
          down vote









          sed -e ':begin;s/pattern//2;t begin'


          or without the sed goto:



          sed -e 's/(pattern)/1n/;h;s/.*n//;s/pattern//g;H;g;s/n.*n//'


          The generic solutions to remove from the nth (3 for example) position are:



          sed -e ':begin;s/pattern//4;t begin'
          sed -e 's/(pattern)/1n/;h;s/.*n//3;s/pattern//g;H;g;s/n.*n//'





          share|improve this answer














          sed -e ':begin;s/pattern//2;t begin'


          or without the sed goto:



          sed -e 's/(pattern)/1n/;h;s/.*n//;s/pattern//g;H;g;s/n.*n//'


          The generic solutions to remove from the nth (3 for example) position are:



          sed -e ':begin;s/pattern//4;t begin'
          sed -e 's/(pattern)/1n/;h;s/.*n//3;s/pattern//g;H;g;s/n.*n//'






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Aug 30 at 23:06









          Isaac

          7,18111035




          7,18111035










          answered Aug 8 '11 at 11:50









          jfg956

          3,10711223




          3,10711223




















              up vote
              1
              down vote













              A slight variation on @jillagre's answer (modified for robustness) could look like:



              sed 's/p(attern)/pn1/;s///g;s/n//'


              ...but in some seds you may need to replace the n in the right-hand side of the first s///ubstitution statement with a literal newline character.






              share|improve this answer
























                up vote
                1
                down vote













                A slight variation on @jillagre's answer (modified for robustness) could look like:



                sed 's/p(attern)/pn1/;s///g;s/n//'


                ...but in some seds you may need to replace the n in the right-hand side of the first s///ubstitution statement with a literal newline character.






                share|improve this answer






















                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  A slight variation on @jillagre's answer (modified for robustness) could look like:



                  sed 's/p(attern)/pn1/;s///g;s/n//'


                  ...but in some seds you may need to replace the n in the right-hand side of the first s///ubstitution statement with a literal newline character.






                  share|improve this answer












                  A slight variation on @jillagre's answer (modified for robustness) could look like:



                  sed 's/p(attern)/pn1/;s///g;s/n//'


                  ...but in some seds you may need to replace the n in the right-hand side of the first s///ubstitution statement with a literal newline character.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 19 '14 at 13:32









                  mikeserv

                  44.4k564150




                  44.4k564150



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f18303%2fsed-delete-all-occurrences-of-a-string-except-the-first-one%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Popular posts from this blog

                      Peggy Mitchell

                      Palaiologos

                      The Forum (Inglewood, California)