Remove multi-line comments

Solution 1:

Here's one way using GNU sed. Run like sed -rf script.sed file.txt

Contents of script.sed:

:a
s%(.*)/\*.*\*/%\1%
ta
/\/\*/ !b
N
ba

Alternatively, here's the one liner:

sed -r ':a; s%(.*)/\*.*\*/%\1%; ta; /\/\*/ !b; N; ba' file.txt

Solution 2:

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.

Solution 3:

This should do

 sed 's|/\*|\n&|g;s|*/|&\n|g' a.txt | sed '/\/\*/,/*\//d'

For test:

a.txt

/* Line test
multi
comment */
Hello there
this would stay 
/* this would be deleteed */

Command:

$ sed 's|/\*|\n&|g;s|*/|&\n|g' a.txt | sed '/\/\*/,/*\//d'
Hello there
this would stay 

Solution 4:

This might work for you (GNU sed):

sed -r ':a;$!{N;ba};s|/\*[^*]*\*+([^/*][^*]*\*+)*/||' file

It's a start, anyway!