Retrieve all the comments from a large C project
I have several directories full of C code, and I want to retrieve all of the /* */ comments from all of the code (.h files and .c files) in all of the directories and subdirectories, putting those comments all in a textfile (things don't need to be particularly orderly, just开发者_Go百科 cram 'em all in there). How can I do this?
Any solution that's scriptable would be great...
Here is a sed script, which prints all the /* */ comments but it will print "comments" in strings (as pointed out by Thomas Matthews) and it will not print // comments. Don't know what happens with nestet comments.
/\/\*/{
bc
:a
s/\///;Tz
:c
s/^[^\/]*//;tb;Tb
:b
s/^\/\*/\/\*/;Ta
:e
s/\*\//\*\//;Td;s/\*\//\*\/\n/;x;tg
:g
s/\(.\)/\1/;Tf;p;s/^.*$//
:f
x;P;s/^.*\n//;bc
:d
H;n;be
:z
}
To print all comments for all files in a directory, use it as follows:
for i in `find -iname "*.c" -or -iname "*.h"`; do echo ">>> $i"; sed -nf the.script.from.above < $i; done > output.txt
This won't handle filenames with whitespace in it and the output will not be pretty!
Cheers
精彩评论