Hi,
grail is right. Whenever it comes to tasks that require counting, 'sed' would not be the first tool of choice. The commands can get quite obfuscated.
However, this is one of the reasons I still like to think about possible sed solutions every now and then; it makes you think "out of the box" - even from a programmer's point of view.
Since you did not provide some representative sample data I had to guess - hope I guessed right:
Code:
%%startPageSetup
some stuff
%%EndPageSetup
%%startPageSetup
some stuff
%%EndPageSetup
%%startPageSetup
some stuff
%%EndPageSetup
%%startPageSetup
some stuff
%%EndPageSetup
%%startPageSetup
some stuff
%%EndPageSetup
%%startPageSetup
some stuff
%%EndPageSetup
And here is how you could process the above sample:
Code:
sed -r '/%%EndPageSetup/ {x;/%%EndPageSetup[[:blank:]]*$/ {s/(%%EndPageSetup).*/\1++/;h;b};g}' file