tags:

views:

134

answers:

1

I am trying to replace a block in a file using sed, but with no success.

E.g. I want to replace this:

for(int i = 0; i < MAX_LOOPS; ++i) {
    printf("Hello World!");
}

With this:

int i = 0;
while(i < MAX_LOOPS) {
    printf("Hello World!");
    ++i;
}
+1  A: 

The following will do the trick but in a very basic way, i.e. it's not robust nor does it worry with formatting.

s/for(int i = 0; i < MAX_LOOPS; ++i)/int i = 0;\nwhile(i < MAX_LOOPS)/g /printf("Hello World!");/{ a++i; }

This is just a quick hack but it ought to be enough to get you going. Sed may not be the best tool for this job but it doesn't hurt to know how to do it in Sed.