convert C style comments to C++ style using sed












6















I'm trying to convert single line 'C' style comments to 'C++' style. The 'sed' below isn't bad, but of course it fails if any leading code (code before the comment) has any ' / ' in it at all:



sed -i 's,(^[^/]*)/*([^*]*)*/[ ]*$,1//2,' filename


What I wish I could do is this:



... [^\/\*] ...


i.e. negate ' /* ' which doesn't work of course, but after several hours of searching, I can't find a simple explanation of how to do that properly :( It doesn't seem like it should be rocket science.



For example, these strings:



blah blah        /* comment */
blah blah / blah /* comment */
blah blah /* comment */ blah
blah blah / blah /* comment */ blah


... should convert thusly:



blah blah        // comment 
blah blah / blah // comment
blah blah /* comment */ blah (this CAN'T be converted)
blah blah / blah /* comment */ blah (this CAN'T be converted)


... obviously no conversion can take place if there is code AFTER the 'C' comment.



I will do a close visual comparison between the file, before and after, so there's no need to handle ' /* ' inside a literal, nor do I want to convert anything multi-line.



Note I think of this as a 'negation' problem but maybe there is another way. I just need to capture everything before a ' /* ' and I don't care how.



FOLLOW UP ON ANSWER BELOW



Well damn! I see that I've completely misunderstood something fundamental:



.*/*


... reads: "anything except slash star followed by slash star", so actually I get my 'negation' for free :-)



So, going even further than Barmar:



sed -i 's,^(.*)/*(.*)*/s*$,1//2,' filename


... will even catch this:



blah / * blah        /* co / mme * nt */


and output this:



blah / * blah       // co / mme * nt 


Enlightenment.










share|improve this question

























  • Why do you need to negate anything before the /*? Just capture everything before /*.

    – Barmar
    Oct 22 '14 at 19:25






  • 1





    How will you handle converting multi-line /* */ comments? What about literal strings that happen to contain "... /* ..."?

    – Greg Hewgill
    Oct 22 '14 at 19:27











  • Please edit your question and show us an example of your input and your desired output. Don't assume that text parsing experts are necessarily familiar with C or C++ syntax.

    – terdon
    Oct 22 '14 at 19:32






  • 3





    Regular expressions are useful for context-free grammars. C comments are not context-free. As comments can be in comments. /* can be in strings, but are not comments. etc. This question is asked regularly, unfortunately I can not remember where. The answers will tell you that regexps can not do it alone. Therefore you will need something more powerful such as awk.

    – ctrl-alt-delor
    Oct 22 '14 at 19:33






  • 1





    @mikeserv, right you are, but your way fails to convert if there's any star or slash in the leading code, whereas my way turns " /* /* comment */ " into the illegal " /* // comment " (which would at least flag a compiler error). More study ...

    – Ray Andrews
    Oct 23 '14 at 15:48


















6















I'm trying to convert single line 'C' style comments to 'C++' style. The 'sed' below isn't bad, but of course it fails if any leading code (code before the comment) has any ' / ' in it at all:



sed -i 's,(^[^/]*)/*([^*]*)*/[ ]*$,1//2,' filename


What I wish I could do is this:



... [^\/\*] ...


i.e. negate ' /* ' which doesn't work of course, but after several hours of searching, I can't find a simple explanation of how to do that properly :( It doesn't seem like it should be rocket science.



For example, these strings:



blah blah        /* comment */
blah blah / blah /* comment */
blah blah /* comment */ blah
blah blah / blah /* comment */ blah


... should convert thusly:



blah blah        // comment 
blah blah / blah // comment
blah blah /* comment */ blah (this CAN'T be converted)
blah blah / blah /* comment */ blah (this CAN'T be converted)


... obviously no conversion can take place if there is code AFTER the 'C' comment.



I will do a close visual comparison between the file, before and after, so there's no need to handle ' /* ' inside a literal, nor do I want to convert anything multi-line.



Note I think of this as a 'negation' problem but maybe there is another way. I just need to capture everything before a ' /* ' and I don't care how.



FOLLOW UP ON ANSWER BELOW



Well damn! I see that I've completely misunderstood something fundamental:



.*/*


... reads: "anything except slash star followed by slash star", so actually I get my 'negation' for free :-)



So, going even further than Barmar:



sed -i 's,^(.*)/*(.*)*/s*$,1//2,' filename


... will even catch this:



blah / * blah        /* co / mme * nt */


and output this:



blah / * blah       // co / mme * nt 


Enlightenment.










share|improve this question

























  • Why do you need to negate anything before the /*? Just capture everything before /*.

    – Barmar
    Oct 22 '14 at 19:25






  • 1





    How will you handle converting multi-line /* */ comments? What about literal strings that happen to contain "... /* ..."?

    – Greg Hewgill
    Oct 22 '14 at 19:27











  • Please edit your question and show us an example of your input and your desired output. Don't assume that text parsing experts are necessarily familiar with C or C++ syntax.

    – terdon
    Oct 22 '14 at 19:32






  • 3





    Regular expressions are useful for context-free grammars. C comments are not context-free. As comments can be in comments. /* can be in strings, but are not comments. etc. This question is asked regularly, unfortunately I can not remember where. The answers will tell you that regexps can not do it alone. Therefore you will need something more powerful such as awk.

    – ctrl-alt-delor
    Oct 22 '14 at 19:33






  • 1





    @mikeserv, right you are, but your way fails to convert if there's any star or slash in the leading code, whereas my way turns " /* /* comment */ " into the illegal " /* // comment " (which would at least flag a compiler error). More study ...

    – Ray Andrews
    Oct 23 '14 at 15:48
















6












6








6








I'm trying to convert single line 'C' style comments to 'C++' style. The 'sed' below isn't bad, but of course it fails if any leading code (code before the comment) has any ' / ' in it at all:



sed -i 's,(^[^/]*)/*([^*]*)*/[ ]*$,1//2,' filename


What I wish I could do is this:



... [^\/\*] ...


i.e. negate ' /* ' which doesn't work of course, but after several hours of searching, I can't find a simple explanation of how to do that properly :( It doesn't seem like it should be rocket science.



For example, these strings:



blah blah        /* comment */
blah blah / blah /* comment */
blah blah /* comment */ blah
blah blah / blah /* comment */ blah


... should convert thusly:



blah blah        // comment 
blah blah / blah // comment
blah blah /* comment */ blah (this CAN'T be converted)
blah blah / blah /* comment */ blah (this CAN'T be converted)


... obviously no conversion can take place if there is code AFTER the 'C' comment.



I will do a close visual comparison between the file, before and after, so there's no need to handle ' /* ' inside a literal, nor do I want to convert anything multi-line.



Note I think of this as a 'negation' problem but maybe there is another way. I just need to capture everything before a ' /* ' and I don't care how.



FOLLOW UP ON ANSWER BELOW



Well damn! I see that I've completely misunderstood something fundamental:



.*/*


... reads: "anything except slash star followed by slash star", so actually I get my 'negation' for free :-)



So, going even further than Barmar:



sed -i 's,^(.*)/*(.*)*/s*$,1//2,' filename


... will even catch this:



blah / * blah        /* co / mme * nt */


and output this:



blah / * blah       // co / mme * nt 


Enlightenment.










share|improve this question
















I'm trying to convert single line 'C' style comments to 'C++' style. The 'sed' below isn't bad, but of course it fails if any leading code (code before the comment) has any ' / ' in it at all:



sed -i 's,(^[^/]*)/*([^*]*)*/[ ]*$,1//2,' filename


What I wish I could do is this:



... [^\/\*] ...


i.e. negate ' /* ' which doesn't work of course, but after several hours of searching, I can't find a simple explanation of how to do that properly :( It doesn't seem like it should be rocket science.



For example, these strings:



blah blah        /* comment */
blah blah / blah /* comment */
blah blah /* comment */ blah
blah blah / blah /* comment */ blah


... should convert thusly:



blah blah        // comment 
blah blah / blah // comment
blah blah /* comment */ blah (this CAN'T be converted)
blah blah / blah /* comment */ blah (this CAN'T be converted)


... obviously no conversion can take place if there is code AFTER the 'C' comment.



I will do a close visual comparison between the file, before and after, so there's no need to handle ' /* ' inside a literal, nor do I want to convert anything multi-line.



Note I think of this as a 'negation' problem but maybe there is another way. I just need to capture everything before a ' /* ' and I don't care how.



FOLLOW UP ON ANSWER BELOW



Well damn! I see that I've completely misunderstood something fundamental:



.*/*


... reads: "anything except slash star followed by slash star", so actually I get my 'negation' for free :-)



So, going even further than Barmar:



sed -i 's,^(.*)/*(.*)*/s*$,1//2,' filename


... will even catch this:



blah / * blah        /* co / mme * nt */


and output this:



blah / * blah       // co / mme * nt 


Enlightenment.







sed






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Oct 16 '16 at 13:16









Jeff Schaller

43.3k1159139




43.3k1159139










asked Oct 22 '14 at 19:11









Ray AndrewsRay Andrews

7703826




7703826













  • Why do you need to negate anything before the /*? Just capture everything before /*.

    – Barmar
    Oct 22 '14 at 19:25






  • 1





    How will you handle converting multi-line /* */ comments? What about literal strings that happen to contain "... /* ..."?

    – Greg Hewgill
    Oct 22 '14 at 19:27











  • Please edit your question and show us an example of your input and your desired output. Don't assume that text parsing experts are necessarily familiar with C or C++ syntax.

    – terdon
    Oct 22 '14 at 19:32






  • 3





    Regular expressions are useful for context-free grammars. C comments are not context-free. As comments can be in comments. /* can be in strings, but are not comments. etc. This question is asked regularly, unfortunately I can not remember where. The answers will tell you that regexps can not do it alone. Therefore you will need something more powerful such as awk.

    – ctrl-alt-delor
    Oct 22 '14 at 19:33






  • 1





    @mikeserv, right you are, but your way fails to convert if there's any star or slash in the leading code, whereas my way turns " /* /* comment */ " into the illegal " /* // comment " (which would at least flag a compiler error). More study ...

    – Ray Andrews
    Oct 23 '14 at 15:48





















  • Why do you need to negate anything before the /*? Just capture everything before /*.

    – Barmar
    Oct 22 '14 at 19:25






  • 1





    How will you handle converting multi-line /* */ comments? What about literal strings that happen to contain "... /* ..."?

    – Greg Hewgill
    Oct 22 '14 at 19:27











  • Please edit your question and show us an example of your input and your desired output. Don't assume that text parsing experts are necessarily familiar with C or C++ syntax.

    – terdon
    Oct 22 '14 at 19:32






  • 3





    Regular expressions are useful for context-free grammars. C comments are not context-free. As comments can be in comments. /* can be in strings, but are not comments. etc. This question is asked regularly, unfortunately I can not remember where. The answers will tell you that regexps can not do it alone. Therefore you will need something more powerful such as awk.

    – ctrl-alt-delor
    Oct 22 '14 at 19:33






  • 1





    @mikeserv, right you are, but your way fails to convert if there's any star or slash in the leading code, whereas my way turns " /* /* comment */ " into the illegal " /* // comment " (which would at least flag a compiler error). More study ...

    – Ray Andrews
    Oct 23 '14 at 15:48



















Why do you need to negate anything before the /*? Just capture everything before /*.

– Barmar
Oct 22 '14 at 19:25





Why do you need to negate anything before the /*? Just capture everything before /*.

– Barmar
Oct 22 '14 at 19:25




1




1





How will you handle converting multi-line /* */ comments? What about literal strings that happen to contain "... /* ..."?

– Greg Hewgill
Oct 22 '14 at 19:27





How will you handle converting multi-line /* */ comments? What about literal strings that happen to contain "... /* ..."?

– Greg Hewgill
Oct 22 '14 at 19:27













Please edit your question and show us an example of your input and your desired output. Don't assume that text parsing experts are necessarily familiar with C or C++ syntax.

– terdon
Oct 22 '14 at 19:32





Please edit your question and show us an example of your input and your desired output. Don't assume that text parsing experts are necessarily familiar with C or C++ syntax.

– terdon
Oct 22 '14 at 19:32




3




3





Regular expressions are useful for context-free grammars. C comments are not context-free. As comments can be in comments. /* can be in strings, but are not comments. etc. This question is asked regularly, unfortunately I can not remember where. The answers will tell you that regexps can not do it alone. Therefore you will need something more powerful such as awk.

– ctrl-alt-delor
Oct 22 '14 at 19:33





Regular expressions are useful for context-free grammars. C comments are not context-free. As comments can be in comments. /* can be in strings, but are not comments. etc. This question is asked regularly, unfortunately I can not remember where. The answers will tell you that regexps can not do it alone. Therefore you will need something more powerful such as awk.

– ctrl-alt-delor
Oct 22 '14 at 19:33




1




1





@mikeserv, right you are, but your way fails to convert if there's any star or slash in the leading code, whereas my way turns " /* /* comment */ " into the illegal " /* // comment " (which would at least flag a compiler error). More study ...

– Ray Andrews
Oct 23 '14 at 15:48







@mikeserv, right you are, but your way fails to convert if there's any star or slash in the leading code, whereas my way turns " /* /* comment */ " into the illegal " /* // comment " (which would at least flag a compiler error). More study ...

– Ray Andrews
Oct 23 '14 at 15:48












3 Answers
3






active

oldest

votes


















4














Try this:



sed 's,^(.*)/*([^/]*)*/$,1//2,'


This won't convert comments that contain embedded / characters. Alternatively, you could use:



sed 's,^(.*)/*(.*)*/$,1//2,'


This will do the wrong thing if you have two comments on the same line, e.g.



blah blah        /* comment1 */ blah /* comment2 */


will convert to



blah blah       // comment1 */ blah /* comment2


It might be possible to do better with a PCRE version of sed, as you could then use negative lookahead to test for embedded comments.



Note also that using , as the delimiter in the s command means that you don't have to escape all the / characters in the regexp -- that's the point of using some character other than / as the delimiter when the regexp will contain lots of /.






share|improve this answer


























  • Indeed! the slash a delimiter seems to me to be a lousy choice, I prefer the comma.

    – Ray Andrews
    Oct 23 '14 at 1:00











  • It all depends on the data you're working on. If it's English text, comma may be a poor choice.

    – Barmar
    Oct 23 '14 at 1:03



















2














Probably the safest way is to first test for lines you don't want to affect and branch out of the script if you have a match.



sed '|*/.*/*|b'


That's a little hard to read with all of the *stars in there, but basically if /* occurs after */ sed will quit executing its script, autoprint the line, and pull in the next line to begin the next line cycle. Any commands following that are not executed for a matching line.



Another way to do this is with test, which will similarly branch out of a script if it is provided no branch label following a successful s///ubstitution:



sed 's|/*|&|2;t'


That attempts to replace the second occurrence of the pattern on the line with itself, and, if successful, it branches out in the same manner b does.



And so...



sed 's|/*|&|2;s|*/|&|2;t
s|/*(.*)*/ *$|//1|'


...will replace the first and only occurrence of /* with // on lines which end with the first and only occurrence of */ and any amount of trailing space characters. This works because t applies to any substitution occurring before it, and so if one or the other tests successful, sed branches out.



It may be that I blunder here, though, as I'm not very familiar with C or C++ and am uncertain what might happen in a /*.**/.**/ case - which the above script branches away from . Perhaps you should instead be testing for only 2 */ or only 2 /*. Hopefully, at least though, I have managed to convey the concept to one who knows better.






share|improve this answer

































    0














    I happened to need the above - but also multiline - so I merged Barmar's answer with some sed of my own to achieve this



    sed -e '//*/,/*//{s/^( *)/*/1~~/g;s/^( *) *//1~~/g;s/^( *) */1~~/g;s/~~////g};s/**////g;s,^(.*)/*(.*)*/s*$,1//2,'


    (BTW if on a mac, you need gsed else you will get an error from the above)



    Here is a diff before/after



    ***************
    *** 1,13 ****

    ! /*
    FOO
    ! */

    ! /*
    ! * BAR
    ! */

    ! blah blah /* comment */
    ! blah blah / blah /* comment */
    blah blah /* comment */ blah
    blah blah / blah /* comment */ blah
    --- 1,13 ----

    ! //
    FOO
    ! //

    ! //
    ! // BAR
    ! //

    ! blah blah // comment
    ! blah blah / blah // comment
    blah blah /* comment */ blah
    blah blah / blah /* comment */ blah





    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%2f163669%2fconvert-c-style-comments-to-c-style-using-sed%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      4














      Try this:



      sed 's,^(.*)/*([^/]*)*/$,1//2,'


      This won't convert comments that contain embedded / characters. Alternatively, you could use:



      sed 's,^(.*)/*(.*)*/$,1//2,'


      This will do the wrong thing if you have two comments on the same line, e.g.



      blah blah        /* comment1 */ blah /* comment2 */


      will convert to



      blah blah       // comment1 */ blah /* comment2


      It might be possible to do better with a PCRE version of sed, as you could then use negative lookahead to test for embedded comments.



      Note also that using , as the delimiter in the s command means that you don't have to escape all the / characters in the regexp -- that's the point of using some character other than / as the delimiter when the regexp will contain lots of /.






      share|improve this answer


























      • Indeed! the slash a delimiter seems to me to be a lousy choice, I prefer the comma.

        – Ray Andrews
        Oct 23 '14 at 1:00











      • It all depends on the data you're working on. If it's English text, comma may be a poor choice.

        – Barmar
        Oct 23 '14 at 1:03
















      4














      Try this:



      sed 's,^(.*)/*([^/]*)*/$,1//2,'


      This won't convert comments that contain embedded / characters. Alternatively, you could use:



      sed 's,^(.*)/*(.*)*/$,1//2,'


      This will do the wrong thing if you have two comments on the same line, e.g.



      blah blah        /* comment1 */ blah /* comment2 */


      will convert to



      blah blah       // comment1 */ blah /* comment2


      It might be possible to do better with a PCRE version of sed, as you could then use negative lookahead to test for embedded comments.



      Note also that using , as the delimiter in the s command means that you don't have to escape all the / characters in the regexp -- that's the point of using some character other than / as the delimiter when the regexp will contain lots of /.






      share|improve this answer


























      • Indeed! the slash a delimiter seems to me to be a lousy choice, I prefer the comma.

        – Ray Andrews
        Oct 23 '14 at 1:00











      • It all depends on the data you're working on. If it's English text, comma may be a poor choice.

        – Barmar
        Oct 23 '14 at 1:03














      4












      4








      4







      Try this:



      sed 's,^(.*)/*([^/]*)*/$,1//2,'


      This won't convert comments that contain embedded / characters. Alternatively, you could use:



      sed 's,^(.*)/*(.*)*/$,1//2,'


      This will do the wrong thing if you have two comments on the same line, e.g.



      blah blah        /* comment1 */ blah /* comment2 */


      will convert to



      blah blah       // comment1 */ blah /* comment2


      It might be possible to do better with a PCRE version of sed, as you could then use negative lookahead to test for embedded comments.



      Note also that using , as the delimiter in the s command means that you don't have to escape all the / characters in the regexp -- that's the point of using some character other than / as the delimiter when the regexp will contain lots of /.






      share|improve this answer















      Try this:



      sed 's,^(.*)/*([^/]*)*/$,1//2,'


      This won't convert comments that contain embedded / characters. Alternatively, you could use:



      sed 's,^(.*)/*(.*)*/$,1//2,'


      This will do the wrong thing if you have two comments on the same line, e.g.



      blah blah        /* comment1 */ blah /* comment2 */


      will convert to



      blah blah       // comment1 */ blah /* comment2


      It might be possible to do better with a PCRE version of sed, as you could then use negative lookahead to test for embedded comments.



      Note also that using , as the delimiter in the s command means that you don't have to escape all the / characters in the regexp -- that's the point of using some character other than / as the delimiter when the regexp will contain lots of /.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Oct 22 '14 at 23:39

























      answered Oct 22 '14 at 21:39









      BarmarBarmar

      7,0821424




      7,0821424













      • Indeed! the slash a delimiter seems to me to be a lousy choice, I prefer the comma.

        – Ray Andrews
        Oct 23 '14 at 1:00











      • It all depends on the data you're working on. If it's English text, comma may be a poor choice.

        – Barmar
        Oct 23 '14 at 1:03



















      • Indeed! the slash a delimiter seems to me to be a lousy choice, I prefer the comma.

        – Ray Andrews
        Oct 23 '14 at 1:00











      • It all depends on the data you're working on. If it's English text, comma may be a poor choice.

        – Barmar
        Oct 23 '14 at 1:03

















      Indeed! the slash a delimiter seems to me to be a lousy choice, I prefer the comma.

      – Ray Andrews
      Oct 23 '14 at 1:00





      Indeed! the slash a delimiter seems to me to be a lousy choice, I prefer the comma.

      – Ray Andrews
      Oct 23 '14 at 1:00













      It all depends on the data you're working on. If it's English text, comma may be a poor choice.

      – Barmar
      Oct 23 '14 at 1:03





      It all depends on the data you're working on. If it's English text, comma may be a poor choice.

      – Barmar
      Oct 23 '14 at 1:03













      2














      Probably the safest way is to first test for lines you don't want to affect and branch out of the script if you have a match.



      sed '|*/.*/*|b'


      That's a little hard to read with all of the *stars in there, but basically if /* occurs after */ sed will quit executing its script, autoprint the line, and pull in the next line to begin the next line cycle. Any commands following that are not executed for a matching line.



      Another way to do this is with test, which will similarly branch out of a script if it is provided no branch label following a successful s///ubstitution:



      sed 's|/*|&|2;t'


      That attempts to replace the second occurrence of the pattern on the line with itself, and, if successful, it branches out in the same manner b does.



      And so...



      sed 's|/*|&|2;s|*/|&|2;t
      s|/*(.*)*/ *$|//1|'


      ...will replace the first and only occurrence of /* with // on lines which end with the first and only occurrence of */ and any amount of trailing space characters. This works because t applies to any substitution occurring before it, and so if one or the other tests successful, sed branches out.



      It may be that I blunder here, though, as I'm not very familiar with C or C++ and am uncertain what might happen in a /*.**/.**/ case - which the above script branches away from . Perhaps you should instead be testing for only 2 */ or only 2 /*. Hopefully, at least though, I have managed to convey the concept to one who knows better.






      share|improve this answer






























        2














        Probably the safest way is to first test for lines you don't want to affect and branch out of the script if you have a match.



        sed '|*/.*/*|b'


        That's a little hard to read with all of the *stars in there, but basically if /* occurs after */ sed will quit executing its script, autoprint the line, and pull in the next line to begin the next line cycle. Any commands following that are not executed for a matching line.



        Another way to do this is with test, which will similarly branch out of a script if it is provided no branch label following a successful s///ubstitution:



        sed 's|/*|&|2;t'


        That attempts to replace the second occurrence of the pattern on the line with itself, and, if successful, it branches out in the same manner b does.



        And so...



        sed 's|/*|&|2;s|*/|&|2;t
        s|/*(.*)*/ *$|//1|'


        ...will replace the first and only occurrence of /* with // on lines which end with the first and only occurrence of */ and any amount of trailing space characters. This works because t applies to any substitution occurring before it, and so if one or the other tests successful, sed branches out.



        It may be that I blunder here, though, as I'm not very familiar with C or C++ and am uncertain what might happen in a /*.**/.**/ case - which the above script branches away from . Perhaps you should instead be testing for only 2 */ or only 2 /*. Hopefully, at least though, I have managed to convey the concept to one who knows better.






        share|improve this answer




























          2












          2








          2







          Probably the safest way is to first test for lines you don't want to affect and branch out of the script if you have a match.



          sed '|*/.*/*|b'


          That's a little hard to read with all of the *stars in there, but basically if /* occurs after */ sed will quit executing its script, autoprint the line, and pull in the next line to begin the next line cycle. Any commands following that are not executed for a matching line.



          Another way to do this is with test, which will similarly branch out of a script if it is provided no branch label following a successful s///ubstitution:



          sed 's|/*|&|2;t'


          That attempts to replace the second occurrence of the pattern on the line with itself, and, if successful, it branches out in the same manner b does.



          And so...



          sed 's|/*|&|2;s|*/|&|2;t
          s|/*(.*)*/ *$|//1|'


          ...will replace the first and only occurrence of /* with // on lines which end with the first and only occurrence of */ and any amount of trailing space characters. This works because t applies to any substitution occurring before it, and so if one or the other tests successful, sed branches out.



          It may be that I blunder here, though, as I'm not very familiar with C or C++ and am uncertain what might happen in a /*.**/.**/ case - which the above script branches away from . Perhaps you should instead be testing for only 2 */ or only 2 /*. Hopefully, at least though, I have managed to convey the concept to one who knows better.






          share|improve this answer















          Probably the safest way is to first test for lines you don't want to affect and branch out of the script if you have a match.



          sed '|*/.*/*|b'


          That's a little hard to read with all of the *stars in there, but basically if /* occurs after */ sed will quit executing its script, autoprint the line, and pull in the next line to begin the next line cycle. Any commands following that are not executed for a matching line.



          Another way to do this is with test, which will similarly branch out of a script if it is provided no branch label following a successful s///ubstitution:



          sed 's|/*|&|2;t'


          That attempts to replace the second occurrence of the pattern on the line with itself, and, if successful, it branches out in the same manner b does.



          And so...



          sed 's|/*|&|2;s|*/|&|2;t
          s|/*(.*)*/ *$|//1|'


          ...will replace the first and only occurrence of /* with // on lines which end with the first and only occurrence of */ and any amount of trailing space characters. This works because t applies to any substitution occurring before it, and so if one or the other tests successful, sed branches out.



          It may be that I blunder here, though, as I'm not very familiar with C or C++ and am uncertain what might happen in a /*.**/.**/ case - which the above script branches away from . Perhaps you should instead be testing for only 2 */ or only 2 /*. Hopefully, at least though, I have managed to convey the concept to one who knows better.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Oct 23 '14 at 8:17

























          answered Oct 23 '14 at 7:55









          mikeservmikeserv

          45.9k668160




          45.9k668160























              0














              I happened to need the above - but also multiline - so I merged Barmar's answer with some sed of my own to achieve this



              sed -e '//*/,/*//{s/^( *)/*/1~~/g;s/^( *) *//1~~/g;s/^( *) */1~~/g;s/~~////g};s/**////g;s,^(.*)/*(.*)*/s*$,1//2,'


              (BTW if on a mac, you need gsed else you will get an error from the above)



              Here is a diff before/after



              ***************
              *** 1,13 ****

              ! /*
              FOO
              ! */

              ! /*
              ! * BAR
              ! */

              ! blah blah /* comment */
              ! blah blah / blah /* comment */
              blah blah /* comment */ blah
              blah blah / blah /* comment */ blah
              --- 1,13 ----

              ! //
              FOO
              ! //

              ! //
              ! // BAR
              ! //

              ! blah blah // comment
              ! blah blah / blah // comment
              blah blah /* comment */ blah
              blah blah / blah /* comment */ blah





              share|improve this answer




























                0














                I happened to need the above - but also multiline - so I merged Barmar's answer with some sed of my own to achieve this



                sed -e '//*/,/*//{s/^( *)/*/1~~/g;s/^( *) *//1~~/g;s/^( *) */1~~/g;s/~~////g};s/**////g;s,^(.*)/*(.*)*/s*$,1//2,'


                (BTW if on a mac, you need gsed else you will get an error from the above)



                Here is a diff before/after



                ***************
                *** 1,13 ****

                ! /*
                FOO
                ! */

                ! /*
                ! * BAR
                ! */

                ! blah blah /* comment */
                ! blah blah / blah /* comment */
                blah blah /* comment */ blah
                blah blah / blah /* comment */ blah
                --- 1,13 ----

                ! //
                FOO
                ! //

                ! //
                ! // BAR
                ! //

                ! blah blah // comment
                ! blah blah / blah // comment
                blah blah /* comment */ blah
                blah blah / blah /* comment */ blah





                share|improve this answer


























                  0












                  0








                  0







                  I happened to need the above - but also multiline - so I merged Barmar's answer with some sed of my own to achieve this



                  sed -e '//*/,/*//{s/^( *)/*/1~~/g;s/^( *) *//1~~/g;s/^( *) */1~~/g;s/~~////g};s/**////g;s,^(.*)/*(.*)*/s*$,1//2,'


                  (BTW if on a mac, you need gsed else you will get an error from the above)



                  Here is a diff before/after



                  ***************
                  *** 1,13 ****

                  ! /*
                  FOO
                  ! */

                  ! /*
                  ! * BAR
                  ! */

                  ! blah blah /* comment */
                  ! blah blah / blah /* comment */
                  blah blah /* comment */ blah
                  blah blah / blah /* comment */ blah
                  --- 1,13 ----

                  ! //
                  FOO
                  ! //

                  ! //
                  ! // BAR
                  ! //

                  ! blah blah // comment
                  ! blah blah / blah // comment
                  blah blah /* comment */ blah
                  blah blah / blah /* comment */ blah





                  share|improve this answer













                  I happened to need the above - but also multiline - so I merged Barmar's answer with some sed of my own to achieve this



                  sed -e '//*/,/*//{s/^( *)/*/1~~/g;s/^( *) *//1~~/g;s/^( *) */1~~/g;s/~~////g};s/**////g;s,^(.*)/*(.*)*/s*$,1//2,'


                  (BTW if on a mac, you need gsed else you will get an error from the above)



                  Here is a diff before/after



                  ***************
                  *** 1,13 ****

                  ! /*
                  FOO
                  ! */

                  ! /*
                  ! * BAR
                  ! */

                  ! blah blah /* comment */
                  ! blah blah / blah /* comment */
                  blah blah /* comment */ blah
                  blah blah / blah /* comment */ blah
                  --- 1,13 ----

                  ! //
                  FOO
                  ! //

                  ! //
                  ! // BAR
                  ! //

                  ! blah blah // comment
                  ! blah blah / blah // comment
                  blah blah /* comment */ blah
                  blah blah / blah /* comment */ blah






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Feb 21 at 16:38









                  Neil McGillNeil McGill

                  29328




                  29328






























                      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%2f163669%2fconvert-c-style-comments-to-c-style-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 make a Squid Proxy server?

                      Is this a new Fibonacci Identity?

                      19世紀