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.
This might work for you (GNU sed):
sed -r ':a;$!{N;ba};s|/\*[^*]*\*+([^/*][^*]*\*+)*/||' file
It's a start, anyway!
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With