Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove multi-line comments

Tags:

sed

awk

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.

like image 393
shantanuo Avatar asked Oct 25 '12 04:10

shantanuo


3 Answers

This might work for you (GNU sed):

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

It's a start, anyway!

like image 133
potong Avatar answered Sep 20 '22 19:09

potong


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
like image 26
Steve Avatar answered Sep 20 '22 19:09

Steve


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 
like image 36
Anshu Avatar answered Sep 23 '22 19:09

Anshu