user3451371
user3451371

Reputation: 39

Removing of multi line comments while preserving the line numbers

I am using this to remove the multiline comments.

awk '/^--/ {next} /^\/\*/ && /\*\/$/{next} /^\/\*/{c=1;next} /\*\/$/{c=0;next} !c{FS="--|\/\*";a=$1;FS="\\*\\/";a=a$2;$0=a;print}' filename

Input is like this

aaaaaaa
/*bbbbb*/
ccccccc
/*bbbbb
bbbbbb*/
ddddd

I want it to be like

aaaaaaa 

ccccccc


dddddd

but im getting

aaaaaaa
ccccccc
ddddddd

Can anyone pls help me ....

Thanks

Upvotes: 1

Views: 58

Answers (2)

Jotne
Jotne

Reputation: 41456

This awk may be the way to go:

awk '/\/\*|\*\// {$0=""}1' file
aaaaaaa

ccccccc


ddddd

It just replace line with /* or */ with nothing.


If the bbbbbb goes over more than one start/stop line, like this:

cat file
aaaaaaa
/*bbbbb*/
ccccccc
/*bbbbb
bbbbbb
bbbbbb*/
ddddd

And you would like to remove all from /* to */ and not only line with these tags:

awk '/^\/\*/ {f=1} {print f?"":$0} /\*\/$/ {f=0}' file
aaaaaaa

ccccccc



ddddd

Upvotes: 1

John1024
John1024

Reputation: 113844

Your code makes several assumptions about how comments are formatted. I am going to assume that your assumptions are correct for your code and just supply the missing blank lines:

$ awk '/^--/ {next} /^\/\*/ && /\*\/$/{print ""; next} /^\/\*/{c=1;print "";next} /\*\/$/{c=0;print "";next} !c '  filename
aaaaaaa

ccccccc


ddddd

Upvotes: 0

Related Questions