views:

167

answers:

3

I need to remove ";" from statements in C which have ";;". Eg:

main()
{
    int i;;
    int j,k;;
    int l;
    for(i=0;i<10;;)
    {}
}

should become:

main()
{
    int i;
    int j,k;
    int l;
    for(i=0;i<10;;)
    {}
}
A: 

Consider using Find & replace command. Any decent text editor has one (Notepad++, KWrite, not to mention Emacs). Search for ;; and replace it with ;.

If you have plenty of code and ; can happen inside char* values, use regexp :)

EDIT: Fine, you should search for: ;; *\n and then replace it with ;\n.

Abgan
NOT to mention emacs? BLASPHEMER
Daniel
:-D That was intentional :-D Always happy to amuse others :-D
Abgan
+1  A: 

UltraEdit could be a good choice..

dincer80
+1  A: 

My first answer was wrong because I missed the ;; in the head of the for loop, which does serve a purpose.

The following perl script will replace all ;; followed by whitespace with a single ;. It will do that for all C files (file extension .c) in the current directory and its subdirectories.

 perl -i -p -e 's/;;(\s*)$/;$1/g' `find | grep .c`

Thanks to Sean Bright for fixing my original mistake. (See the comments.)

I just want to point out that the extra ; at the end of line shouldn't really present a problem. You could just leave them there, or you could just use your text editor to search and replace the ones that are extraneous.

Bill the Lizard
The ;; would be replaced in the for loop, which isn't what he wants.
strager
But is that what he wants? Its not clear.
Simon Knights
@strager: Thanks, I missed that in the original format of the code.
Bill the Lizard
@Bill: with a slight mod your example would work: perl -i -p -e 's/;;\s*$/;/g' `find | grep .c`
Sean Bright
@Sean: Thanks, I modified it even further to preserve the whitespace.
Bill the Lizard
@Bill: I considered that, but trailing whitespace is icky :P
Sean Bright
@Sean: I think my solution will work. It just puts the same whitespace back in after replacing ;; with a single ;. I've only tried it with the OP's example, though.
Bill the Lizard
@Bill: the * needs to go inside the parens, but otherwise it looks good (I don't feel comfortable editting other people's answers)
Sean Bright
Thanks, that works beautifully.
Bill the Lizard