954,157 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

how to delete the special lines in all cpp files?

Some of my source files include the following code.

#ifndef AAAAAAAAAAAA
#define AAAAAAAAAAAA
  #if !defined(__lint) && !defined(_lint)
    XXXXXXXXXXXXXXXXXXXXXXXXX;
  #endif
#endif


I want to delete line1, 2, 6. So, Could somebody tell me how to do it? thank you in advance!

littlestone
Light Poster
42 posts since Mar 2008
Reputation Points: 14
Solved Threads: 6
 

Hey There,

If you just want to delete files that (I'm assuming) begin with a pound character and start with ifndef,define or endif, you could do it (not very generically in this example) by using sed:

$ sed '/^#[ifndef|define|endif]/d' FILENAME #if !defined(__lint) && !defined(_lint) XXXXXXXXXXXXXXXXXXXXXXXXX; #endif

You can also be more generic so that you delete more or less lines with less code by using regular expressions. This should take care of your immediate problem, though.

Happy THanksgiving :)

, Mike

eggi
Posting Pro in Training
400 posts since Oct 2007
Reputation Points: 102
Solved Threads: 47
 

Hi eggi,

Thank you for your reply. I don't want to remove all the line that begin with a pound character and then start with ifndef,define or endif. I just want to remove the three lines in the 6 lines.

In addition, I don't know why your the sed command remove all the lines begin with a pound character. such as "#include aaaa.h"

littlestone
Light Poster
42 posts since Mar 2008
Reputation Points: 14
Solved Threads: 6
 

Hey again,

In that case it might just be simpler to do:

sed -e 1,2d -e 6d FILENAME

The reason the command above removes any line starting with a pound is that I write answers in too many different languages ;) The proper syntax for this command (although, actually, it was syntactically correct (no errors) but didn't do what was expected) would be to do:

sed '/^#\(ifndef\|define\|endif\)/d' FILENAME

Best wishes,

Mike

eggi
Posting Pro in Training
400 posts since Oct 2007
Reputation Points: 102
Solved Threads: 47
 

Thank you so much.

littlestone
Light Poster
42 posts since Mar 2008
Reputation Points: 14
Solved Threads: 6
 

Glad to help out :)

Best wishes,

Mike

eggi
Posting Pro in Training
400 posts since Oct 2007
Reputation Points: 102
Solved Threads: 47
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You