SED command to comment all the lines except the one's that matched the pattern

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











up vote
1
down vote

favorite












I have the below cron.txt file



58 18 * * 1-5 /usr/home/script.sh REP CXC BS TODAY all 1>/dev/null 2>/dev/null
00 19 * * 1-5 /usr/home/script.sh DSC DXC BUS TODAY all 1>/dev/null 2>/dev/null
01 19 * * 1-5 /usr/home/script.sh REP HP SNT TODAY all 1>/dev/null 2>/dev/null
03 19 * * 1-5 /usr/home/script.sh DSC CXC SNT TODAY all 1>/dev/null 2>/dev/null
32 10 * * 1-5 /usr/home/script.sh Check CXC OD TODAY MGLA 1>/dev/null 2>/dev/null
32 12 * * 1-5 /usr/home/script.sh Sca CXC OD TODAY all "01 03 05 07 08 10 12 17 18 19 31 32 33 37 42 50 53 55 57 84 89 93" 1>/dev/null 2>/dev/null
01 19 * * 1-5 /usr/home/script.sh REP HK SNT TODAY all 1>/dev/null 2>/dev/null
01 19 * * 1-5 /usr/home/script.sh REP RAM SNT TODAY all 1>/dev/null 2>/dev/null
01 19 * * 1-5 /usr/home/script.sh REP SAB SNT TODAY all 1>/dev/null 2>/dev/null


And I need to comment (add #) at the beginning of all the lines that doesn't have pattern CXC or HP in it.



I tried



grep -iwvE "CXC|HP" cron.txt | sed 's/^/#/g' > cron.txt_bkp


It didn't work as expected.







share|improve this question


























    up vote
    1
    down vote

    favorite












    I have the below cron.txt file



    58 18 * * 1-5 /usr/home/script.sh REP CXC BS TODAY all 1>/dev/null 2>/dev/null
    00 19 * * 1-5 /usr/home/script.sh DSC DXC BUS TODAY all 1>/dev/null 2>/dev/null
    01 19 * * 1-5 /usr/home/script.sh REP HP SNT TODAY all 1>/dev/null 2>/dev/null
    03 19 * * 1-5 /usr/home/script.sh DSC CXC SNT TODAY all 1>/dev/null 2>/dev/null
    32 10 * * 1-5 /usr/home/script.sh Check CXC OD TODAY MGLA 1>/dev/null 2>/dev/null
    32 12 * * 1-5 /usr/home/script.sh Sca CXC OD TODAY all "01 03 05 07 08 10 12 17 18 19 31 32 33 37 42 50 53 55 57 84 89 93" 1>/dev/null 2>/dev/null
    01 19 * * 1-5 /usr/home/script.sh REP HK SNT TODAY all 1>/dev/null 2>/dev/null
    01 19 * * 1-5 /usr/home/script.sh REP RAM SNT TODAY all 1>/dev/null 2>/dev/null
    01 19 * * 1-5 /usr/home/script.sh REP SAB SNT TODAY all 1>/dev/null 2>/dev/null


    And I need to comment (add #) at the beginning of all the lines that doesn't have pattern CXC or HP in it.



    I tried



    grep -iwvE "CXC|HP" cron.txt | sed 's/^/#/g' > cron.txt_bkp


    It didn't work as expected.







    share|improve this question
























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I have the below cron.txt file



      58 18 * * 1-5 /usr/home/script.sh REP CXC BS TODAY all 1>/dev/null 2>/dev/null
      00 19 * * 1-5 /usr/home/script.sh DSC DXC BUS TODAY all 1>/dev/null 2>/dev/null
      01 19 * * 1-5 /usr/home/script.sh REP HP SNT TODAY all 1>/dev/null 2>/dev/null
      03 19 * * 1-5 /usr/home/script.sh DSC CXC SNT TODAY all 1>/dev/null 2>/dev/null
      32 10 * * 1-5 /usr/home/script.sh Check CXC OD TODAY MGLA 1>/dev/null 2>/dev/null
      32 12 * * 1-5 /usr/home/script.sh Sca CXC OD TODAY all "01 03 05 07 08 10 12 17 18 19 31 32 33 37 42 50 53 55 57 84 89 93" 1>/dev/null 2>/dev/null
      01 19 * * 1-5 /usr/home/script.sh REP HK SNT TODAY all 1>/dev/null 2>/dev/null
      01 19 * * 1-5 /usr/home/script.sh REP RAM SNT TODAY all 1>/dev/null 2>/dev/null
      01 19 * * 1-5 /usr/home/script.sh REP SAB SNT TODAY all 1>/dev/null 2>/dev/null


      And I need to comment (add #) at the beginning of all the lines that doesn't have pattern CXC or HP in it.



      I tried



      grep -iwvE "CXC|HP" cron.txt | sed 's/^/#/g' > cron.txt_bkp


      It didn't work as expected.







      share|improve this question














      I have the below cron.txt file



      58 18 * * 1-5 /usr/home/script.sh REP CXC BS TODAY all 1>/dev/null 2>/dev/null
      00 19 * * 1-5 /usr/home/script.sh DSC DXC BUS TODAY all 1>/dev/null 2>/dev/null
      01 19 * * 1-5 /usr/home/script.sh REP HP SNT TODAY all 1>/dev/null 2>/dev/null
      03 19 * * 1-5 /usr/home/script.sh DSC CXC SNT TODAY all 1>/dev/null 2>/dev/null
      32 10 * * 1-5 /usr/home/script.sh Check CXC OD TODAY MGLA 1>/dev/null 2>/dev/null
      32 12 * * 1-5 /usr/home/script.sh Sca CXC OD TODAY all "01 03 05 07 08 10 12 17 18 19 31 32 33 37 42 50 53 55 57 84 89 93" 1>/dev/null 2>/dev/null
      01 19 * * 1-5 /usr/home/script.sh REP HK SNT TODAY all 1>/dev/null 2>/dev/null
      01 19 * * 1-5 /usr/home/script.sh REP RAM SNT TODAY all 1>/dev/null 2>/dev/null
      01 19 * * 1-5 /usr/home/script.sh REP SAB SNT TODAY all 1>/dev/null 2>/dev/null


      And I need to comment (add #) at the beginning of all the lines that doesn't have pattern CXC or HP in it.



      I tried



      grep -iwvE "CXC|HP" cron.txt | sed 's/^/#/g' > cron.txt_bkp


      It didn't work as expected.









      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 30 at 9:58









      Kusalananda

      103k13202318




      103k13202318










      asked Jan 30 at 5:34









      sabarish jackson

      119111




      119111




















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          4
          down vote













          Assuming that your implementation of grep supports both the -w and the -E flag, your grep and sed pipeline would only extract and comment out the lines that didn't contain HP or CXC. The lines that were to be left as-is will not be transferred to the new file.




          Since HP-UX sed seems to not support alteration with | in regular expressions, which means that /CXC|HP/!s/^/# / won't work, you may apply something like this to the file:



          sed -e '/CXC/b' -e '/HP/b' -e 's/^/# /' crontab.txt >crontab-new.txt


          The b command makes sed branch to the end of the editing script if the pattern is found on the current line. It acts as a "print line and continue with next line" command here.



          If neither of the first two expressions are acted upon, then the line is commented out by the last expression.



          The file crontab-new.txt, created above, will hold the following contents given the example in the question:



          58 18 * * 1-5 /usr/home/script.sh REP CXC BS TODAY all 1>/dev/null 2>/dev/null
          # 00 19 * * 1-5 /usr/home/script.sh DSC DXC BUS TODAY all 1>/dev/null 2>/dev/null
          01 19 * * 1-5 /usr/home/script.sh REP HP SNT TODAY all 1>/dev/null 2>/dev/null
          03 19 * * 1-5 /usr/home/script.sh DSC CXC SNT TODAY all 1>/dev/null 2>/dev/null
          32 10 * * 1-5 /usr/home/script.sh Check CXC OD TODAY MGLA 1>/dev/null 2>/dev/null
          32 12 * * 1-5 /usr/home/script.sh Sca CXC OD TODAY all "01 03 05 07 08 10 12 17 18 19 31 32 33 37 42 50 53 55 57 84 89 93" 1>/dev/null 2>/dev/null
          # 01 19 * * 1-5 /usr/home/script.sh REP HK SNT TODAY all 1>/dev/null 2>/dev/null
          # 01 19 * * 1-5 /usr/home/script.sh REP RAM SNT TODAY all 1>/dev/null 2>/dev/null
          # 01 19 * * 1-5 /usr/home/script.sh REP SAB SNT TODAY all 1>/dev/null 2>/dev/null





          share|improve this answer


















          • 1




            I think you misread the question. You got it backwards.
            – Wildcard
            Jan 30 at 7:39










          • @Wildcard Now properly corrected.
            – Kusalananda
            Jan 30 at 8:47

















          up vote
          1
          down vote













          A solution with awk:



          awk '!/CXC|HP/$0="#" $01' cron.txt > cron.txt_bkp





          share|improve this answer


















          • 1




            gsub() could be changed to sub(). There is only one single match of ^ in a line... Alternatively, into just $0 = "#" $0.
            – Kusalananda
            Jan 30 at 10:57


















          up vote
          0
          down vote













          I have used below sed command to achieve the same



          cron.txt



          58 18 * * 1-5 /usr/home/script.sh REP CXC BS TODAY all 1>/dev/null 2>/dev/null
          00 19 * * 1-5 /usr/home/script.sh DSC DXC BUS TODAY all 1>/dev/null 2>/dev/null
          01 19 * * 1-5 /usr/home/script.sh REP HP SNT TODAY all 1>/dev/null 2>/dev/null
          03 19 * * 1-5 /usr/home/script.sh DSC CXC SNT TODAY all 1>/dev/null 2>/dev/null
          32 10 * * 1-5 /usr/home/script.sh Check CXC OD TODAY MGLA 1>/dev/null 2>/dev/null
          32 12 * * 1-5 /usr/home/script.sh Sca CXC OD TODAY all "01 03 05 07 08 10 12 17 18 19 31 32 33 37 42 50 53 55 57 84 89 93" 1>/dev/null 2>/dev/null
          01 19 * * 1-5 /usr/home/script.sh REP HK SNT TODAY all 1>/dev/null 2>/dev/null
          01 19 * * 1-5 /usr/home/script.sh REP RAM SNT TODAY all 1>/dev/null 2>/dev/null
          01 19 * * 1-5 /usr/home/script.sh REP SAB SNT TODAY all 1>/dev/null 2>/dev/null


          command



          sed '/HP/!s/^/#/g' cron.txt | sed '/CXC/s/^#//g' >cron.txt_bkp


          output



          58 18 * * 1-5 /usr/home/script.sh REP CXC BS TODAY all 1>/dev/null 2>/dev/null
          #00 19 * * 1-5 /usr/home/script.sh DSC DXC BUS TODAY all 1>/dev/null 2>/dev/null
          01 19 * * 1-5 /usr/home/script.sh REP HP SNT TODAY all 1>/dev/null 2>/dev/null
          03 19 * * 1-5 /usr/home/script.sh DSC CXC SNT TODAY all 1>/dev/null 2>/dev/null
          32 10 * * 1-5 /usr/home/script.sh Check CXC OD TODAY MGLA 1>/dev/null 2>/dev/null
          32 12 * * 1-5 /usr/home/script.sh Sca CXC OD TODAY all "01 03 05 07 08 10 12 17 18 19 31 32 33 37 42 50 53 55 57 84 89 93" 1>/dev/null 2>/dev/null
          #01 19 * * 1-5 /usr/home/script.sh REP HK SNT TODAY all 1>/dev/null 2>/dev/null
          #01 19 * * 1-5 /usr/home/script.sh REP RAM SNT TODAY all 1>/dev/null 2>/dev/null
          #01 19 * * 1-5 /usr/home/script.sh REP SAB SNT TODAY all 1>/dev/null 2>/dev/null





          share|improve this answer




















          • This will definitely work, but it's a tiny bit roundabout way of doing it (adding, then removing comments).
            – Kusalananda
            Jan 31 at 9:13










          • Kindly vote for my answer
            – Praveen Kumar BS
            Jan 31 at 9:52










          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%2f420586%2fsed-command-to-comment-all-the-lines-except-the-ones-that-matched-the-pattern%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
          4
          down vote













          Assuming that your implementation of grep supports both the -w and the -E flag, your grep and sed pipeline would only extract and comment out the lines that didn't contain HP or CXC. The lines that were to be left as-is will not be transferred to the new file.




          Since HP-UX sed seems to not support alteration with | in regular expressions, which means that /CXC|HP/!s/^/# / won't work, you may apply something like this to the file:



          sed -e '/CXC/b' -e '/HP/b' -e 's/^/# /' crontab.txt >crontab-new.txt


          The b command makes sed branch to the end of the editing script if the pattern is found on the current line. It acts as a "print line and continue with next line" command here.



          If neither of the first two expressions are acted upon, then the line is commented out by the last expression.



          The file crontab-new.txt, created above, will hold the following contents given the example in the question:



          58 18 * * 1-5 /usr/home/script.sh REP CXC BS TODAY all 1>/dev/null 2>/dev/null
          # 00 19 * * 1-5 /usr/home/script.sh DSC DXC BUS TODAY all 1>/dev/null 2>/dev/null
          01 19 * * 1-5 /usr/home/script.sh REP HP SNT TODAY all 1>/dev/null 2>/dev/null
          03 19 * * 1-5 /usr/home/script.sh DSC CXC SNT TODAY all 1>/dev/null 2>/dev/null
          32 10 * * 1-5 /usr/home/script.sh Check CXC OD TODAY MGLA 1>/dev/null 2>/dev/null
          32 12 * * 1-5 /usr/home/script.sh Sca CXC OD TODAY all "01 03 05 07 08 10 12 17 18 19 31 32 33 37 42 50 53 55 57 84 89 93" 1>/dev/null 2>/dev/null
          # 01 19 * * 1-5 /usr/home/script.sh REP HK SNT TODAY all 1>/dev/null 2>/dev/null
          # 01 19 * * 1-5 /usr/home/script.sh REP RAM SNT TODAY all 1>/dev/null 2>/dev/null
          # 01 19 * * 1-5 /usr/home/script.sh REP SAB SNT TODAY all 1>/dev/null 2>/dev/null





          share|improve this answer


















          • 1




            I think you misread the question. You got it backwards.
            – Wildcard
            Jan 30 at 7:39










          • @Wildcard Now properly corrected.
            – Kusalananda
            Jan 30 at 8:47














          up vote
          4
          down vote













          Assuming that your implementation of grep supports both the -w and the -E flag, your grep and sed pipeline would only extract and comment out the lines that didn't contain HP or CXC. The lines that were to be left as-is will not be transferred to the new file.




          Since HP-UX sed seems to not support alteration with | in regular expressions, which means that /CXC|HP/!s/^/# / won't work, you may apply something like this to the file:



          sed -e '/CXC/b' -e '/HP/b' -e 's/^/# /' crontab.txt >crontab-new.txt


          The b command makes sed branch to the end of the editing script if the pattern is found on the current line. It acts as a "print line and continue with next line" command here.



          If neither of the first two expressions are acted upon, then the line is commented out by the last expression.



          The file crontab-new.txt, created above, will hold the following contents given the example in the question:



          58 18 * * 1-5 /usr/home/script.sh REP CXC BS TODAY all 1>/dev/null 2>/dev/null
          # 00 19 * * 1-5 /usr/home/script.sh DSC DXC BUS TODAY all 1>/dev/null 2>/dev/null
          01 19 * * 1-5 /usr/home/script.sh REP HP SNT TODAY all 1>/dev/null 2>/dev/null
          03 19 * * 1-5 /usr/home/script.sh DSC CXC SNT TODAY all 1>/dev/null 2>/dev/null
          32 10 * * 1-5 /usr/home/script.sh Check CXC OD TODAY MGLA 1>/dev/null 2>/dev/null
          32 12 * * 1-5 /usr/home/script.sh Sca CXC OD TODAY all "01 03 05 07 08 10 12 17 18 19 31 32 33 37 42 50 53 55 57 84 89 93" 1>/dev/null 2>/dev/null
          # 01 19 * * 1-5 /usr/home/script.sh REP HK SNT TODAY all 1>/dev/null 2>/dev/null
          # 01 19 * * 1-5 /usr/home/script.sh REP RAM SNT TODAY all 1>/dev/null 2>/dev/null
          # 01 19 * * 1-5 /usr/home/script.sh REP SAB SNT TODAY all 1>/dev/null 2>/dev/null





          share|improve this answer


















          • 1




            I think you misread the question. You got it backwards.
            – Wildcard
            Jan 30 at 7:39










          • @Wildcard Now properly corrected.
            – Kusalananda
            Jan 30 at 8:47












          up vote
          4
          down vote










          up vote
          4
          down vote









          Assuming that your implementation of grep supports both the -w and the -E flag, your grep and sed pipeline would only extract and comment out the lines that didn't contain HP or CXC. The lines that were to be left as-is will not be transferred to the new file.




          Since HP-UX sed seems to not support alteration with | in regular expressions, which means that /CXC|HP/!s/^/# / won't work, you may apply something like this to the file:



          sed -e '/CXC/b' -e '/HP/b' -e 's/^/# /' crontab.txt >crontab-new.txt


          The b command makes sed branch to the end of the editing script if the pattern is found on the current line. It acts as a "print line and continue with next line" command here.



          If neither of the first two expressions are acted upon, then the line is commented out by the last expression.



          The file crontab-new.txt, created above, will hold the following contents given the example in the question:



          58 18 * * 1-5 /usr/home/script.sh REP CXC BS TODAY all 1>/dev/null 2>/dev/null
          # 00 19 * * 1-5 /usr/home/script.sh DSC DXC BUS TODAY all 1>/dev/null 2>/dev/null
          01 19 * * 1-5 /usr/home/script.sh REP HP SNT TODAY all 1>/dev/null 2>/dev/null
          03 19 * * 1-5 /usr/home/script.sh DSC CXC SNT TODAY all 1>/dev/null 2>/dev/null
          32 10 * * 1-5 /usr/home/script.sh Check CXC OD TODAY MGLA 1>/dev/null 2>/dev/null
          32 12 * * 1-5 /usr/home/script.sh Sca CXC OD TODAY all "01 03 05 07 08 10 12 17 18 19 31 32 33 37 42 50 53 55 57 84 89 93" 1>/dev/null 2>/dev/null
          # 01 19 * * 1-5 /usr/home/script.sh REP HK SNT TODAY all 1>/dev/null 2>/dev/null
          # 01 19 * * 1-5 /usr/home/script.sh REP RAM SNT TODAY all 1>/dev/null 2>/dev/null
          # 01 19 * * 1-5 /usr/home/script.sh REP SAB SNT TODAY all 1>/dev/null 2>/dev/null





          share|improve this answer














          Assuming that your implementation of grep supports both the -w and the -E flag, your grep and sed pipeline would only extract and comment out the lines that didn't contain HP or CXC. The lines that were to be left as-is will not be transferred to the new file.




          Since HP-UX sed seems to not support alteration with | in regular expressions, which means that /CXC|HP/!s/^/# / won't work, you may apply something like this to the file:



          sed -e '/CXC/b' -e '/HP/b' -e 's/^/# /' crontab.txt >crontab-new.txt


          The b command makes sed branch to the end of the editing script if the pattern is found on the current line. It acts as a "print line and continue with next line" command here.



          If neither of the first two expressions are acted upon, then the line is commented out by the last expression.



          The file crontab-new.txt, created above, will hold the following contents given the example in the question:



          58 18 * * 1-5 /usr/home/script.sh REP CXC BS TODAY all 1>/dev/null 2>/dev/null
          # 00 19 * * 1-5 /usr/home/script.sh DSC DXC BUS TODAY all 1>/dev/null 2>/dev/null
          01 19 * * 1-5 /usr/home/script.sh REP HP SNT TODAY all 1>/dev/null 2>/dev/null
          03 19 * * 1-5 /usr/home/script.sh DSC CXC SNT TODAY all 1>/dev/null 2>/dev/null
          32 10 * * 1-5 /usr/home/script.sh Check CXC OD TODAY MGLA 1>/dev/null 2>/dev/null
          32 12 * * 1-5 /usr/home/script.sh Sca CXC OD TODAY all "01 03 05 07 08 10 12 17 18 19 31 32 33 37 42 50 53 55 57 84 89 93" 1>/dev/null 2>/dev/null
          # 01 19 * * 1-5 /usr/home/script.sh REP HK SNT TODAY all 1>/dev/null 2>/dev/null
          # 01 19 * * 1-5 /usr/home/script.sh REP RAM SNT TODAY all 1>/dev/null 2>/dev/null
          # 01 19 * * 1-5 /usr/home/script.sh REP SAB SNT TODAY all 1>/dev/null 2>/dev/null






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 30 at 10:55

























          answered Jan 30 at 7:37









          Kusalananda

          103k13202318




          103k13202318







          • 1




            I think you misread the question. You got it backwards.
            – Wildcard
            Jan 30 at 7:39










          • @Wildcard Now properly corrected.
            – Kusalananda
            Jan 30 at 8:47












          • 1




            I think you misread the question. You got it backwards.
            – Wildcard
            Jan 30 at 7:39










          • @Wildcard Now properly corrected.
            – Kusalananda
            Jan 30 at 8:47







          1




          1




          I think you misread the question. You got it backwards.
          – Wildcard
          Jan 30 at 7:39




          I think you misread the question. You got it backwards.
          – Wildcard
          Jan 30 at 7:39












          @Wildcard Now properly corrected.
          – Kusalananda
          Jan 30 at 8:47




          @Wildcard Now properly corrected.
          – Kusalananda
          Jan 30 at 8:47












          up vote
          1
          down vote













          A solution with awk:



          awk '!/CXC|HP/$0="#" $01' cron.txt > cron.txt_bkp





          share|improve this answer


















          • 1




            gsub() could be changed to sub(). There is only one single match of ^ in a line... Alternatively, into just $0 = "#" $0.
            – Kusalananda
            Jan 30 at 10:57















          up vote
          1
          down vote













          A solution with awk:



          awk '!/CXC|HP/$0="#" $01' cron.txt > cron.txt_bkp





          share|improve this answer


















          • 1




            gsub() could be changed to sub(). There is only one single match of ^ in a line... Alternatively, into just $0 = "#" $0.
            – Kusalananda
            Jan 30 at 10:57













          up vote
          1
          down vote










          up vote
          1
          down vote









          A solution with awk:



          awk '!/CXC|HP/$0="#" $01' cron.txt > cron.txt_bkp





          share|improve this answer














          A solution with awk:



          awk '!/CXC|HP/$0="#" $01' cron.txt > cron.txt_bkp






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 30 at 12:46

























          answered Jan 30 at 5:53









          Isaac

          6,7021734




          6,7021734







          • 1




            gsub() could be changed to sub(). There is only one single match of ^ in a line... Alternatively, into just $0 = "#" $0.
            – Kusalananda
            Jan 30 at 10:57













          • 1




            gsub() could be changed to sub(). There is only one single match of ^ in a line... Alternatively, into just $0 = "#" $0.
            – Kusalananda
            Jan 30 at 10:57








          1




          1




          gsub() could be changed to sub(). There is only one single match of ^ in a line... Alternatively, into just $0 = "#" $0.
          – Kusalananda
          Jan 30 at 10:57





          gsub() could be changed to sub(). There is only one single match of ^ in a line... Alternatively, into just $0 = "#" $0.
          – Kusalananda
          Jan 30 at 10:57











          up vote
          0
          down vote













          I have used below sed command to achieve the same



          cron.txt



          58 18 * * 1-5 /usr/home/script.sh REP CXC BS TODAY all 1>/dev/null 2>/dev/null
          00 19 * * 1-5 /usr/home/script.sh DSC DXC BUS TODAY all 1>/dev/null 2>/dev/null
          01 19 * * 1-5 /usr/home/script.sh REP HP SNT TODAY all 1>/dev/null 2>/dev/null
          03 19 * * 1-5 /usr/home/script.sh DSC CXC SNT TODAY all 1>/dev/null 2>/dev/null
          32 10 * * 1-5 /usr/home/script.sh Check CXC OD TODAY MGLA 1>/dev/null 2>/dev/null
          32 12 * * 1-5 /usr/home/script.sh Sca CXC OD TODAY all "01 03 05 07 08 10 12 17 18 19 31 32 33 37 42 50 53 55 57 84 89 93" 1>/dev/null 2>/dev/null
          01 19 * * 1-5 /usr/home/script.sh REP HK SNT TODAY all 1>/dev/null 2>/dev/null
          01 19 * * 1-5 /usr/home/script.sh REP RAM SNT TODAY all 1>/dev/null 2>/dev/null
          01 19 * * 1-5 /usr/home/script.sh REP SAB SNT TODAY all 1>/dev/null 2>/dev/null


          command



          sed '/HP/!s/^/#/g' cron.txt | sed '/CXC/s/^#//g' >cron.txt_bkp


          output



          58 18 * * 1-5 /usr/home/script.sh REP CXC BS TODAY all 1>/dev/null 2>/dev/null
          #00 19 * * 1-5 /usr/home/script.sh DSC DXC BUS TODAY all 1>/dev/null 2>/dev/null
          01 19 * * 1-5 /usr/home/script.sh REP HP SNT TODAY all 1>/dev/null 2>/dev/null
          03 19 * * 1-5 /usr/home/script.sh DSC CXC SNT TODAY all 1>/dev/null 2>/dev/null
          32 10 * * 1-5 /usr/home/script.sh Check CXC OD TODAY MGLA 1>/dev/null 2>/dev/null
          32 12 * * 1-5 /usr/home/script.sh Sca CXC OD TODAY all "01 03 05 07 08 10 12 17 18 19 31 32 33 37 42 50 53 55 57 84 89 93" 1>/dev/null 2>/dev/null
          #01 19 * * 1-5 /usr/home/script.sh REP HK SNT TODAY all 1>/dev/null 2>/dev/null
          #01 19 * * 1-5 /usr/home/script.sh REP RAM SNT TODAY all 1>/dev/null 2>/dev/null
          #01 19 * * 1-5 /usr/home/script.sh REP SAB SNT TODAY all 1>/dev/null 2>/dev/null





          share|improve this answer




















          • This will definitely work, but it's a tiny bit roundabout way of doing it (adding, then removing comments).
            – Kusalananda
            Jan 31 at 9:13










          • Kindly vote for my answer
            – Praveen Kumar BS
            Jan 31 at 9:52














          up vote
          0
          down vote













          I have used below sed command to achieve the same



          cron.txt



          58 18 * * 1-5 /usr/home/script.sh REP CXC BS TODAY all 1>/dev/null 2>/dev/null
          00 19 * * 1-5 /usr/home/script.sh DSC DXC BUS TODAY all 1>/dev/null 2>/dev/null
          01 19 * * 1-5 /usr/home/script.sh REP HP SNT TODAY all 1>/dev/null 2>/dev/null
          03 19 * * 1-5 /usr/home/script.sh DSC CXC SNT TODAY all 1>/dev/null 2>/dev/null
          32 10 * * 1-5 /usr/home/script.sh Check CXC OD TODAY MGLA 1>/dev/null 2>/dev/null
          32 12 * * 1-5 /usr/home/script.sh Sca CXC OD TODAY all "01 03 05 07 08 10 12 17 18 19 31 32 33 37 42 50 53 55 57 84 89 93" 1>/dev/null 2>/dev/null
          01 19 * * 1-5 /usr/home/script.sh REP HK SNT TODAY all 1>/dev/null 2>/dev/null
          01 19 * * 1-5 /usr/home/script.sh REP RAM SNT TODAY all 1>/dev/null 2>/dev/null
          01 19 * * 1-5 /usr/home/script.sh REP SAB SNT TODAY all 1>/dev/null 2>/dev/null


          command



          sed '/HP/!s/^/#/g' cron.txt | sed '/CXC/s/^#//g' >cron.txt_bkp


          output



          58 18 * * 1-5 /usr/home/script.sh REP CXC BS TODAY all 1>/dev/null 2>/dev/null
          #00 19 * * 1-5 /usr/home/script.sh DSC DXC BUS TODAY all 1>/dev/null 2>/dev/null
          01 19 * * 1-5 /usr/home/script.sh REP HP SNT TODAY all 1>/dev/null 2>/dev/null
          03 19 * * 1-5 /usr/home/script.sh DSC CXC SNT TODAY all 1>/dev/null 2>/dev/null
          32 10 * * 1-5 /usr/home/script.sh Check CXC OD TODAY MGLA 1>/dev/null 2>/dev/null
          32 12 * * 1-5 /usr/home/script.sh Sca CXC OD TODAY all "01 03 05 07 08 10 12 17 18 19 31 32 33 37 42 50 53 55 57 84 89 93" 1>/dev/null 2>/dev/null
          #01 19 * * 1-5 /usr/home/script.sh REP HK SNT TODAY all 1>/dev/null 2>/dev/null
          #01 19 * * 1-5 /usr/home/script.sh REP RAM SNT TODAY all 1>/dev/null 2>/dev/null
          #01 19 * * 1-5 /usr/home/script.sh REP SAB SNT TODAY all 1>/dev/null 2>/dev/null





          share|improve this answer




















          • This will definitely work, but it's a tiny bit roundabout way of doing it (adding, then removing comments).
            – Kusalananda
            Jan 31 at 9:13










          • Kindly vote for my answer
            – Praveen Kumar BS
            Jan 31 at 9:52












          up vote
          0
          down vote










          up vote
          0
          down vote









          I have used below sed command to achieve the same



          cron.txt



          58 18 * * 1-5 /usr/home/script.sh REP CXC BS TODAY all 1>/dev/null 2>/dev/null
          00 19 * * 1-5 /usr/home/script.sh DSC DXC BUS TODAY all 1>/dev/null 2>/dev/null
          01 19 * * 1-5 /usr/home/script.sh REP HP SNT TODAY all 1>/dev/null 2>/dev/null
          03 19 * * 1-5 /usr/home/script.sh DSC CXC SNT TODAY all 1>/dev/null 2>/dev/null
          32 10 * * 1-5 /usr/home/script.sh Check CXC OD TODAY MGLA 1>/dev/null 2>/dev/null
          32 12 * * 1-5 /usr/home/script.sh Sca CXC OD TODAY all "01 03 05 07 08 10 12 17 18 19 31 32 33 37 42 50 53 55 57 84 89 93" 1>/dev/null 2>/dev/null
          01 19 * * 1-5 /usr/home/script.sh REP HK SNT TODAY all 1>/dev/null 2>/dev/null
          01 19 * * 1-5 /usr/home/script.sh REP RAM SNT TODAY all 1>/dev/null 2>/dev/null
          01 19 * * 1-5 /usr/home/script.sh REP SAB SNT TODAY all 1>/dev/null 2>/dev/null


          command



          sed '/HP/!s/^/#/g' cron.txt | sed '/CXC/s/^#//g' >cron.txt_bkp


          output



          58 18 * * 1-5 /usr/home/script.sh REP CXC BS TODAY all 1>/dev/null 2>/dev/null
          #00 19 * * 1-5 /usr/home/script.sh DSC DXC BUS TODAY all 1>/dev/null 2>/dev/null
          01 19 * * 1-5 /usr/home/script.sh REP HP SNT TODAY all 1>/dev/null 2>/dev/null
          03 19 * * 1-5 /usr/home/script.sh DSC CXC SNT TODAY all 1>/dev/null 2>/dev/null
          32 10 * * 1-5 /usr/home/script.sh Check CXC OD TODAY MGLA 1>/dev/null 2>/dev/null
          32 12 * * 1-5 /usr/home/script.sh Sca CXC OD TODAY all "01 03 05 07 08 10 12 17 18 19 31 32 33 37 42 50 53 55 57 84 89 93" 1>/dev/null 2>/dev/null
          #01 19 * * 1-5 /usr/home/script.sh REP HK SNT TODAY all 1>/dev/null 2>/dev/null
          #01 19 * * 1-5 /usr/home/script.sh REP RAM SNT TODAY all 1>/dev/null 2>/dev/null
          #01 19 * * 1-5 /usr/home/script.sh REP SAB SNT TODAY all 1>/dev/null 2>/dev/null





          share|improve this answer












          I have used below sed command to achieve the same



          cron.txt



          58 18 * * 1-5 /usr/home/script.sh REP CXC BS TODAY all 1>/dev/null 2>/dev/null
          00 19 * * 1-5 /usr/home/script.sh DSC DXC BUS TODAY all 1>/dev/null 2>/dev/null
          01 19 * * 1-5 /usr/home/script.sh REP HP SNT TODAY all 1>/dev/null 2>/dev/null
          03 19 * * 1-5 /usr/home/script.sh DSC CXC SNT TODAY all 1>/dev/null 2>/dev/null
          32 10 * * 1-5 /usr/home/script.sh Check CXC OD TODAY MGLA 1>/dev/null 2>/dev/null
          32 12 * * 1-5 /usr/home/script.sh Sca CXC OD TODAY all "01 03 05 07 08 10 12 17 18 19 31 32 33 37 42 50 53 55 57 84 89 93" 1>/dev/null 2>/dev/null
          01 19 * * 1-5 /usr/home/script.sh REP HK SNT TODAY all 1>/dev/null 2>/dev/null
          01 19 * * 1-5 /usr/home/script.sh REP RAM SNT TODAY all 1>/dev/null 2>/dev/null
          01 19 * * 1-5 /usr/home/script.sh REP SAB SNT TODAY all 1>/dev/null 2>/dev/null


          command



          sed '/HP/!s/^/#/g' cron.txt | sed '/CXC/s/^#//g' >cron.txt_bkp


          output



          58 18 * * 1-5 /usr/home/script.sh REP CXC BS TODAY all 1>/dev/null 2>/dev/null
          #00 19 * * 1-5 /usr/home/script.sh DSC DXC BUS TODAY all 1>/dev/null 2>/dev/null
          01 19 * * 1-5 /usr/home/script.sh REP HP SNT TODAY all 1>/dev/null 2>/dev/null
          03 19 * * 1-5 /usr/home/script.sh DSC CXC SNT TODAY all 1>/dev/null 2>/dev/null
          32 10 * * 1-5 /usr/home/script.sh Check CXC OD TODAY MGLA 1>/dev/null 2>/dev/null
          32 12 * * 1-5 /usr/home/script.sh Sca CXC OD TODAY all "01 03 05 07 08 10 12 17 18 19 31 32 33 37 42 50 53 55 57 84 89 93" 1>/dev/null 2>/dev/null
          #01 19 * * 1-5 /usr/home/script.sh REP HK SNT TODAY all 1>/dev/null 2>/dev/null
          #01 19 * * 1-5 /usr/home/script.sh REP RAM SNT TODAY all 1>/dev/null 2>/dev/null
          #01 19 * * 1-5 /usr/home/script.sh REP SAB SNT TODAY all 1>/dev/null 2>/dev/null






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 30 at 17:52









          Praveen Kumar BS

          1,010128




          1,010128











          • This will definitely work, but it's a tiny bit roundabout way of doing it (adding, then removing comments).
            – Kusalananda
            Jan 31 at 9:13










          • Kindly vote for my answer
            – Praveen Kumar BS
            Jan 31 at 9:52
















          • This will definitely work, but it's a tiny bit roundabout way of doing it (adding, then removing comments).
            – Kusalananda
            Jan 31 at 9:13










          • Kindly vote for my answer
            – Praveen Kumar BS
            Jan 31 at 9:52















          This will definitely work, but it's a tiny bit roundabout way of doing it (adding, then removing comments).
          – Kusalananda
          Jan 31 at 9:13




          This will definitely work, but it's a tiny bit roundabout way of doing it (adding, then removing comments).
          – Kusalananda
          Jan 31 at 9:13












          Kindly vote for my answer
          – Praveen Kumar BS
          Jan 31 at 9:52




          Kindly vote for my answer
          – Praveen Kumar BS
          Jan 31 at 9:52












           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f420586%2fsed-command-to-comment-all-the-lines-except-the-ones-that-matched-the-pattern%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