how to remove comments with sed
Hi, everybody,
I have the following problem: I have a source code with comments. the comment symbols are (* and *) everything between these symbols is a comment. The (* and *) can be on different lines in that case all those lines between (* and *) are comments. I want to delete all comments from the file. I found that it can be done with sed by one-liner like this: # delete section of file between two regular expressions (inclusive) sed -n '/tag1/,/tag2/d' I tried sed -n "/(\*/,/\*)/d" sed -n "/\(\*/,/\*\)/d" sed -n "/{(\*}/,/{\*)}/d" sed -n ":(\*/:,:\*):d" nothing of these worked. So, my question is how to do that? What is the regular expression for tag (* and tag *) Best, Thank you for help. |
Code:
sed -r '/\(\*/,/\*\)/d' odd_comments before or after your comment, too, though. Cheers, Tink |
Code:
awk '/\(\*/{ Code:
# more file |
Hopefully Tink meant Note
I might be inclined to only do this if there was only whitespace before and after the comment indicators. |
Quote:
Uh. This did not work. It does not butcher lines where there's code. It removes all lines everywhere. Very weird indeed. |
Hmmm ...
Code:
~/tmp$ cat odd_comments Cheers, Tink |
Quote:
|
Doesn't he want the output to be
Quote:
|
i would believe so.
|
I'm just guiding him in the use of the sed-invocation
he spotted :D ... |
Neither solution deals with embedded comments in the one line - with data before and/or after. A simple sed piped into ghostdog74 awk offering worked fine.
When it gets to that point, I start to think perl . |
I have to admit to something of a strained relationship with sed across line breaks.
Historically, I've just given up and written something small in python or C. I'd actually suggest a tokenised approach would be best - ie, start at a count of zero, and while the count is zero, output a character, and increment count each time the pattern "(*" is found, and decrement the count each time "*)" is found. Allows for nested comments, etc. Pretty simple to do, and probably quicker than playing around with sed (at least for me). Edit - syg00 beat me to it. |
Quote:
I had hoped that there was sed's one-liner that could do that. It turns out that sed has a defect. :( Not good. We need to find a responsible and severely :D punish him. Where should I file a complaint? :D:D. Could you please explain me how your awk code works? |
'sed -n' only prints lines specified with the p command or the p flag of the s command. So none of the sed commands in the first post will give any output.
For sed '/tag1/,/tag2/d' to work, tag1 and tag2 have to be on different lines. Complete lines are deleted, not just the tags and characters between the tags. The following method needs GNU sed. It replaces *) with \a, a GNU escape extension which produces or matches a bel character, ascii 7. (It doesn't have to be \a; any character which will never be in a comment will do.) Changing the end tag into a single character enables "greedy matching" to be limited using [^\a]*\a ':a N;$!ba' puts the whole file into the pattern space. 's/\*)/\a/g' replaces all *) with \a 's/(\*[^\a]*\a//g' deletes all the comments Code:
sed ':a N;$!ba; s/\*)/\a/g; s/(\*[^\a]*\a//g' infile > outfile |
If loss of formatting (the line-break in the split comment) is
not an issue a perl one liner will do, too. Code:
perl -nle '$/ = "" ; s/\(\*[^\)]+\*\)//gm; print $_' file |
All times are GMT -5. The time now is 08:50 AM. |