views:

15

answers:

1

I know that you can use this to remove blank lines

sed /^$/d

and this to remove comments starting with #

sed /^#/d

but how to you do delete all the comments starting with // ?

+2  A: 

You just need to "escape" the slashes with the backslash.

/\/\//

the ^ operator binds it to the front of the line, so your example will only affect comments starting in the first column. You could try adding spaces and tabs in there, too, and then use the alternation operator | to choose between two comment identifiers.

/^[ \t]*(\/\/|$)/

Edit: If you simply want to remove comments from the file, then you can do something like:

/(\/\/|$).*/

I don't know what the 'd' operator at the end does, but the above expression should match for you modulo having to escape the parentheses or the alternation operator (the '|' character)

Edit 2: I just realized that using a Mac you may be "shelling" that command and using the system sed. In that case, you could try putting quotation marks around the search pattern so that the shell doesn't do anything crazy to all of your magic characters. :) In this case, 'd' means "delete the pattern space," so just stick a 'd' after the last example I gave and you should be set.

Edit 3: Oh I just realized, you'll want to beware that if you don't catch things inside of quotes (i.e. you don't want to delete from # to end of line if it's in a string!). The regexp becomes quite a bit more complicated in that case, unfortunately, unless you just forgo checking lines with strings for comments. ...but then you'd need to use the substitution operation to sed rather than search-and-delete-match. ...and you'd need to put in more escapes, and it becomes madness. I suggest searching for an online sed helper (there are good regex testers out there, maybe there's one for sed?).

Sorry to sort of abandon the project at this point. This "problem" is one that sed can do but it becomes substantially more complex at every stage, as opposed to just whipping up a bit of Python to do it.

dash-tom-bang
got the first one to work, but the second gives me an error. I would like to delete all the comments from the whole document regardless if they are at the start of a line.
timg
I don't know the specifics of TextMate's regex implementation, but you could try to escape the parentheses (you'd have to escape them in default Vim, for instance, to make them work as grouping tokens).
dash-tom-bang