Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8318905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T22:08:18+00:00 2026-06-08T22:08:18+00:00

How can i use sed to replace all my C-style comments in a source

  • 0

How can i use sed to replace all my C-style comments in a source file to C++ style.

All these:

int main() {
  /* some comments */
  ...

to:

int main() {
  // some comments
  ...

All comments are single line and there are none in between code like this:

int f(int x /*x-coordinate*/ );

so I tried this:

 sed -i 's/ \/\* .*  \*\ / \/\/* /g' src.c

but it leaves the file unchanged.
This post is similar, but I’m trying to understand sed’s expression syntax. Since “.” matches any character and ” * ” matches zero or more of some pattern. I assume “.*” matches any number of any character.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T22:08:20+00:00Added an answer on June 8, 2026 at 10:08 pm
    sed -i 's:\(.*\)/[*]\(.*\)[*]/:\1 // \2:' FILE
    

    this will transform each line like this :

    aaa  /* test */
    

    into a line like this:

    aaa  // test
    

    If you have more comments on the same line, you can apply this more sophisticated parser, that converts a line like:

    aaa /* c1 */ bbb /* c2 */ ccc
    

    into

    aaa  bbb ccc // c1 c2
    

    sed -i ':r s:\(.*\)/[*]\(.*\)[*]/\(.*\):\1\3 //\2:;tr;s://\(.*\)//\(.*\)://\2\1:;tr' FILE
    

    A more sophisticated case is when you have comments inside strings on a line, like in call("/*string*/"). Here is a script c-comments.sed, to solve this problem:

    s:\(["][^"]*["]\):\n\1\n:g
    s:/[*]:\n&:g
    s:[*]/:&\n:g
    :r
    s:["]\([^\n]*\)\n\([^"]*\)":"\1\2":g
    tr
    :x
    s:\(.*\)\n/[*]\([^\n]*\)[*]/\n\(.*\)$:\1\3 // \2:
    s:\(.*\)\n\(.*\)//\(.*\)//\(.*\):\1\n\2 //\4\3:
    tx
    s:\n::g
    

    You save this script into a file c-comments.sed, and you call it like this:

    sed -i -f c-comments.sed FILE
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I use sed to replace this line char * path_list_[1] = {
I can use this, to find all instances of fly and replace it with
I know you can use sed to get the nth line of a text
My HTML code has the following line. <TH>column1</TH><TH>column2</TH><TH>column3</TH> Can I use sed tool to
Can I use sed if I need to extract a pattern enclosed by a
How can we use sed to get the 4 characters between Install ID: and
I can't seem to use a variable in a sed command, for example: sed
I can use this maven plugin maven-jaxb-plugin to generate Java Classes from XSD file.
I'm trying to use sed to fix a file, and I'm having trouble grokking
I want a sed script that I can use for 1) finding instances, and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.