2

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 cram 'em all in there). How can I do this?

Any solution that's scriptable would be great...

magnetar
  • 6,487
  • 7
  • 28
  • 40

1 Answers1

0

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

sl0815
  • 567
  • 1
  • 9
  • 21