Change all the // character for commenting in c++ to the c comment characters /* */ [duplicate]

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











up vote
4
down vote

favorite













This question is an exact duplicate of:



  • replacing and adding at the end of lines with one line sed command

    2 answers



I m trying to change some characters in a file, like this:



//this is a thest of how this works
#include <stdio.h>

int main()

// declare some variables here
int num1 = 4;
float num2 = 3.5;

// print the result
printf("The result is %fn", num1 * num2); // this does it

/* does it work? */
return 0;



I want to change all the // character for commenting in c++ to the c comment characters /* */ making the file then look like this:



/* This is a test of how this works */
#include <stdio.h>

/*this is a thest of how this works */
#include <stdio.h>

int main()

/* declare some variables here */
int num1 = 4;
float num2 = 3.5;

/* print the result */
printf("The result is %fn", num1 * num2); /* this does it */

/* does it work? */ */
return 0;



I do not know much bash at all and this is what i came up with sed 's/////* *//g' myprog.c
it did not work, what am i doing wrong or what do i need to do to make these changes? Im trying to make it a one line command










share|improve this question









New contributor




fedora is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











marked as duplicate by don_crissti, Jeff Schaller, Goro, andcoz, Romeo Ninov 17 secs ago


This question was marked as an exact duplicate of an existing question.










  • 1




    This has been asked several times (sounds like a new homework assignment), the right answer is sed is the wrong tool for this job but good luck anyway...
    – don_crissti
    59 mins ago











  • @don_crissti I am so sorry I greatly apologies I didn't know there is an answer for this question
    – fedora
    46 mins ago










  • Related: unix.stackexchange.com/questions/163669/…
    – Jeff Schaller
    21 mins ago










  • and unix.stackexchange.com/q/72429/117549
    – Jeff Schaller
    18 mins ago














up vote
4
down vote

favorite













This question is an exact duplicate of:



  • replacing and adding at the end of lines with one line sed command

    2 answers



I m trying to change some characters in a file, like this:



//this is a thest of how this works
#include <stdio.h>

int main()

// declare some variables here
int num1 = 4;
float num2 = 3.5;

// print the result
printf("The result is %fn", num1 * num2); // this does it

/* does it work? */
return 0;



I want to change all the // character for commenting in c++ to the c comment characters /* */ making the file then look like this:



/* This is a test of how this works */
#include <stdio.h>

/*this is a thest of how this works */
#include <stdio.h>

int main()

/* declare some variables here */
int num1 = 4;
float num2 = 3.5;

/* print the result */
printf("The result is %fn", num1 * num2); /* this does it */

/* does it work? */ */
return 0;



I do not know much bash at all and this is what i came up with sed 's/////* *//g' myprog.c
it did not work, what am i doing wrong or what do i need to do to make these changes? Im trying to make it a one line command










share|improve this question









New contributor




fedora is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











marked as duplicate by don_crissti, Jeff Schaller, Goro, andcoz, Romeo Ninov 17 secs ago


This question was marked as an exact duplicate of an existing question.










  • 1




    This has been asked several times (sounds like a new homework assignment), the right answer is sed is the wrong tool for this job but good luck anyway...
    – don_crissti
    59 mins ago











  • @don_crissti I am so sorry I greatly apologies I didn't know there is an answer for this question
    – fedora
    46 mins ago










  • Related: unix.stackexchange.com/questions/163669/…
    – Jeff Schaller
    21 mins ago










  • and unix.stackexchange.com/q/72429/117549
    – Jeff Schaller
    18 mins ago












up vote
4
down vote

favorite









up vote
4
down vote

favorite












This question is an exact duplicate of:



  • replacing and adding at the end of lines with one line sed command

    2 answers



I m trying to change some characters in a file, like this:



//this is a thest of how this works
#include <stdio.h>

int main()

// declare some variables here
int num1 = 4;
float num2 = 3.5;

// print the result
printf("The result is %fn", num1 * num2); // this does it

/* does it work? */
return 0;



I want to change all the // character for commenting in c++ to the c comment characters /* */ making the file then look like this:



/* This is a test of how this works */
#include <stdio.h>

/*this is a thest of how this works */
#include <stdio.h>

int main()

/* declare some variables here */
int num1 = 4;
float num2 = 3.5;

/* print the result */
printf("The result is %fn", num1 * num2); /* this does it */

/* does it work? */ */
return 0;



I do not know much bash at all and this is what i came up with sed 's/////* *//g' myprog.c
it did not work, what am i doing wrong or what do i need to do to make these changes? Im trying to make it a one line command










share|improve this question









New contributor




fedora is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












This question is an exact duplicate of:



  • replacing and adding at the end of lines with one line sed command

    2 answers



I m trying to change some characters in a file, like this:



//this is a thest of how this works
#include <stdio.h>

int main()

// declare some variables here
int num1 = 4;
float num2 = 3.5;

// print the result
printf("The result is %fn", num1 * num2); // this does it

/* does it work? */
return 0;



I want to change all the // character for commenting in c++ to the c comment characters /* */ making the file then look like this:



/* This is a test of how this works */
#include <stdio.h>

/*this is a thest of how this works */
#include <stdio.h>

int main()

/* declare some variables here */
int num1 = 4;
float num2 = 3.5;

/* print the result */
printf("The result is %fn", num1 * num2); /* this does it */

/* does it work? */ */
return 0;



I do not know much bash at all and this is what i came up with sed 's/////* *//g' myprog.c
it did not work, what am i doing wrong or what do i need to do to make these changes? Im trying to make it a one line command





This question is an exact duplicate of:



  • replacing and adding at the end of lines with one line sed command

    2 answers







sed






share|improve this question









New contributor




fedora is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




fedora is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 40 mins ago









Jeff Schaller

33.7k851113




33.7k851113






New contributor




fedora is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 1 hour ago









fedora

234




234




New contributor




fedora is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





fedora is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






fedora is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




marked as duplicate by don_crissti, Jeff Schaller, Goro, andcoz, Romeo Ninov 17 secs ago


This question was marked as an exact duplicate of an existing question.






marked as duplicate by don_crissti, Jeff Schaller, Goro, andcoz, Romeo Ninov 17 secs ago


This question was marked as an exact duplicate of an existing question.









  • 1




    This has been asked several times (sounds like a new homework assignment), the right answer is sed is the wrong tool for this job but good luck anyway...
    – don_crissti
    59 mins ago











  • @don_crissti I am so sorry I greatly apologies I didn't know there is an answer for this question
    – fedora
    46 mins ago










  • Related: unix.stackexchange.com/questions/163669/…
    – Jeff Schaller
    21 mins ago










  • and unix.stackexchange.com/q/72429/117549
    – Jeff Schaller
    18 mins ago












  • 1




    This has been asked several times (sounds like a new homework assignment), the right answer is sed is the wrong tool for this job but good luck anyway...
    – don_crissti
    59 mins ago











  • @don_crissti I am so sorry I greatly apologies I didn't know there is an answer for this question
    – fedora
    46 mins ago










  • Related: unix.stackexchange.com/questions/163669/…
    – Jeff Schaller
    21 mins ago










  • and unix.stackexchange.com/q/72429/117549
    – Jeff Schaller
    18 mins ago







1




1




This has been asked several times (sounds like a new homework assignment), the right answer is sed is the wrong tool for this job but good luck anyway...
– don_crissti
59 mins ago





This has been asked several times (sounds like a new homework assignment), the right answer is sed is the wrong tool for this job but good luck anyway...
– don_crissti
59 mins ago













@don_crissti I am so sorry I greatly apologies I didn't know there is an answer for this question
– fedora
46 mins ago




@don_crissti I am so sorry I greatly apologies I didn't know there is an answer for this question
– fedora
46 mins ago












Related: unix.stackexchange.com/questions/163669/…
– Jeff Schaller
21 mins ago




Related: unix.stackexchange.com/questions/163669/…
– Jeff Schaller
21 mins ago












and unix.stackexchange.com/q/72429/117549
– Jeff Schaller
18 mins ago




and unix.stackexchange.com/q/72429/117549
– Jeff Schaller
18 mins ago










2 Answers
2






active

oldest

votes

















up vote
3
down vote



accepted










You only need this:



 's+//+/*+g' file | sed 's+/*.*+& */+'


/*this is a thest of how this works */
#include <stdio.h>

int main()

/* declare some variables here */
int num1 = 4;
float num2 = 3.5;

/* print the result */
printf("The result is %fn", num1 * num2); /* this does it */

/* does it work? */ */
return 0;






share|improve this answer






















  • anchoring the // misses the // this does it comment
    – Jeff Schaller
    55 mins ago






  • 1




    @goro. Thank you very much for the help
    – fedora
    46 mins ago

















up vote
1
down vote













sed 's|//(.*)|/*1 */|'


But beware there are a number of cases where it wouldn't do the right thing like in:



char *url = "http://host/";
/*
comment with // nested C++-syle comment
*/
// comment
continued on the next line


To account for those cases and more, you could adapt the code at that other Q&A as:



perl -0777 -pe '
BEGIN
$bs=qr(?:\;
$lc=qr(?:$bsn

s[^rn])*)
defined($1)?"/*$1 */":$&exsg'


Which on the above sample gives:



char *url = "http://host/";
/*
comment with // nested C++-syle comment
*/
/* comment
continued on the next line */





share|improve this answer





























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    3
    down vote



    accepted










    You only need this:



     's+//+/*+g' file | sed 's+/*.*+& */+'


    /*this is a thest of how this works */
    #include <stdio.h>

    int main()

    /* declare some variables here */
    int num1 = 4;
    float num2 = 3.5;

    /* print the result */
    printf("The result is %fn", num1 * num2); /* this does it */

    /* does it work? */ */
    return 0;






    share|improve this answer






















    • anchoring the // misses the // this does it comment
      – Jeff Schaller
      55 mins ago






    • 1




      @goro. Thank you very much for the help
      – fedora
      46 mins ago














    up vote
    3
    down vote



    accepted










    You only need this:



     's+//+/*+g' file | sed 's+/*.*+& */+'


    /*this is a thest of how this works */
    #include <stdio.h>

    int main()

    /* declare some variables here */
    int num1 = 4;
    float num2 = 3.5;

    /* print the result */
    printf("The result is %fn", num1 * num2); /* this does it */

    /* does it work? */ */
    return 0;






    share|improve this answer






















    • anchoring the // misses the // this does it comment
      – Jeff Schaller
      55 mins ago






    • 1




      @goro. Thank you very much for the help
      – fedora
      46 mins ago












    up vote
    3
    down vote



    accepted







    up vote
    3
    down vote



    accepted






    You only need this:



     's+//+/*+g' file | sed 's+/*.*+& */+'


    /*this is a thest of how this works */
    #include <stdio.h>

    int main()

    /* declare some variables here */
    int num1 = 4;
    float num2 = 3.5;

    /* print the result */
    printf("The result is %fn", num1 * num2); /* this does it */

    /* does it work? */ */
    return 0;






    share|improve this answer














    You only need this:



     's+//+/*+g' file | sed 's+/*.*+& */+'


    /*this is a thest of how this works */
    #include <stdio.h>

    int main()

    /* declare some variables here */
    int num1 = 4;
    float num2 = 3.5;

    /* print the result */
    printf("The result is %fn", num1 * num2); /* this does it */

    /* does it work? */ */
    return 0;







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited 44 mins ago

























    answered 1 hour ago









    Goro

    7,52753270




    7,52753270











    • anchoring the // misses the // this does it comment
      – Jeff Schaller
      55 mins ago






    • 1




      @goro. Thank you very much for the help
      – fedora
      46 mins ago
















    • anchoring the // misses the // this does it comment
      – Jeff Schaller
      55 mins ago






    • 1




      @goro. Thank you very much for the help
      – fedora
      46 mins ago















    anchoring the // misses the // this does it comment
    – Jeff Schaller
    55 mins ago




    anchoring the // misses the // this does it comment
    – Jeff Schaller
    55 mins ago




    1




    1




    @goro. Thank you very much for the help
    – fedora
    46 mins ago




    @goro. Thank you very much for the help
    – fedora
    46 mins ago












    up vote
    1
    down vote













    sed 's|//(.*)|/*1 */|'


    But beware there are a number of cases where it wouldn't do the right thing like in:



    char *url = "http://host/";
    /*
    comment with // nested C++-syle comment
    */
    // comment
    continued on the next line


    To account for those cases and more, you could adapt the code at that other Q&A as:



    perl -0777 -pe '
    BEGIN
    $bs=qr(?:\;
    $lc=qr(?:$bsn

    s[^rn])*)
    defined($1)?"/*$1 */":$&exsg'


    Which on the above sample gives:



    char *url = "http://host/";
    /*
    comment with // nested C++-syle comment
    */
    /* comment
    continued on the next line */





    share|improve this answer


























      up vote
      1
      down vote













      sed 's|//(.*)|/*1 */|'


      But beware there are a number of cases where it wouldn't do the right thing like in:



      char *url = "http://host/";
      /*
      comment with // nested C++-syle comment
      */
      // comment
      continued on the next line


      To account for those cases and more, you could adapt the code at that other Q&A as:



      perl -0777 -pe '
      BEGIN
      $bs=qr(?:\;
      $lc=qr(?:$bsn

      s[^rn])*)
      defined($1)?"/*$1 */":$&exsg'


      Which on the above sample gives:



      char *url = "http://host/";
      /*
      comment with // nested C++-syle comment
      */
      /* comment
      continued on the next line */





      share|improve this answer
























        up vote
        1
        down vote










        up vote
        1
        down vote









        sed 's|//(.*)|/*1 */|'


        But beware there are a number of cases where it wouldn't do the right thing like in:



        char *url = "http://host/";
        /*
        comment with // nested C++-syle comment
        */
        // comment
        continued on the next line


        To account for those cases and more, you could adapt the code at that other Q&A as:



        perl -0777 -pe '
        BEGIN
        $bs=qr(?:\;
        $lc=qr(?:$bsn

        s[^rn])*)
        defined($1)?"/*$1 */":$&exsg'


        Which on the above sample gives:



        char *url = "http://host/";
        /*
        comment with // nested C++-syle comment
        */
        /* comment
        continued on the next line */





        share|improve this answer














        sed 's|//(.*)|/*1 */|'


        But beware there are a number of cases where it wouldn't do the right thing like in:



        char *url = "http://host/";
        /*
        comment with // nested C++-syle comment
        */
        // comment
        continued on the next line


        To account for those cases and more, you could adapt the code at that other Q&A as:



        perl -0777 -pe '
        BEGIN
        $bs=qr(?:\;
        $lc=qr(?:$bsn

        s[^rn])*)
        defined($1)?"/*$1 */":$&exsg'


        Which on the above sample gives:



        char *url = "http://host/";
        /*
        comment with // nested C++-syle comment
        */
        /* comment
        continued on the next line */






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 9 mins ago

























        answered 31 mins ago









        Stéphane Chazelas

        287k53531868




        287k53531868












            Comments

            Popular posts from this blog

            What does second last employer means? [closed]

            List of Gilmore Girls characters

            Confectionery