Remove multi-line comments

awk, sed

Solution

If this is in a C file then you MUST use a C preprocessor for this in combination with other tools to temporarily disable specific preprocessor functionality like expanding #defines or #includes, all other approaches will fail in edge cases. This will work for all cases:

[ $# -eq 2 ] && arg="$1" || arg=""
eval file="\$$#"
sed 's/a/aA/g; s/__/aB/g; s/#/aC/g' "$file" |
          gcc -P -E $arg - |
          sed 's/aC/#/g; s/aB/__/g; s/aA/a/g'

Put it in a shell script and call it with the name of the file you want parsed, optionally prefixed by a flag like "-ansi" to specify the C standard to apply.

See https://stackoverflow.com/a/35708616/1745001 for details.

Problem

How do I remove all comments if they start with /* and end with */ I have tried the following. It works for one line comment. ``` sed '/\/\*/d' ``` But it does not remove multiline comments. for e.g. the second and third lines are not removed. ``` /*!50500 PARTITION BY RANGE (TO_SECONDS(date_time )) PARTITION 20120102parti VALUES LESS THAN (63492681600), (PARTITION 20120101parti VALUES LESS THAN (63492595200) */ ; ``` In the above example, I need to retain the last ; after the closing comment sign.

Original source

Related problems