0
First line NC1.c   
/* comments        
second line nc1.c
  */               
*/
adsf
asfd  

I'd like to match the lines between the "/" and the last "/" and delete them, the result is

First line NC1.c   
adsf
asfd  

How should I write the command?

0

Something like this seems ok:

$ sed '/\/\*/,/\*\//d; /\*\//d; /\/\*/d' file4
First line NC1.c   
adsf
asfd 

For inplace editing you can use sed -i

This sed does three separate deletions:

/\/\*/,/\*\//d = Range of lines between pattern /* and */ (escaped with \)
/\*\//d = lines containing */ (escaped)
/\/\*/d = lines containing /* (escaped)

  • You can't do this reliably on a C source code file without a language-aware parser. A simple line containing puts("/*") would screw it up. – Kusalananda Sep 24 '17 at 18:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.