Question

There is a way to remove from a file all rows wrapped between /* and */ using a bash script?

I use percona to generate a sql script to syncronize two databases, a development one to a production one. Percona generates a well formatted SQL script but full of comments which make increase file size. So, just to make easier upload operation I'd prefer to remove all the unnecessary.

EDIT ON January 10th

I solved with this code:

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

thanks all

Was it helpful?

Solution

Using sed:

sed '/\/\*.*\*\// d; /\/\*/,/\*\// d' file

The command d tells sed to delete patterns matching the preceeding expression. The first expression /\/\*.*\*\// matches one-line comments, the second one /\/\*/,/\*\// comments that range multiple lines (this is implied by the ,).

I don't know if this works 100%, but as far as I tried, it did the job.

OTHER TIPS

-Try this script- it should help removing the comments, since are the same as C++ Here you can see another sed example to remove HTML comments

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top