Here is an example block of text I want to format:

[TR]
[TD][/TD]
[TD]tear a cat in, to make all split.[/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD="class: tdci"]The raging rocks[/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD="class: tdci"]The foolish Fates.[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]This was lofty! Now name the rest of the players.[/TD]
[TD][/TD]
[/TR]
using these two 'sed' commands in a script:

sed -ri '/^/N;s/(\n)/\n\ \1/' "$f" #insert table row with empty data fields (blank line) above first line with 'class="tdci"'sed -ri '/^/N;s/(\n)/\n\ \1/' "$f" #insert table row with empty data fields (blank line) after last line with 'class="tdci"'here is the result:

[TR]
[TD][/TD]
[TD]tear a cat in, to make all split.[/TD]
[TD][/TD]
[/TR]
[TR]
[TD] [/TD]
[/TR]
[TR]
[TD][/TD]
[TD="class: tdci"]The raging rocks[/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD="class: tdci"]The foolish Fates.[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]This was lofty! Now name the rest of the players.[/TD]
[TD][/TD]
[/TR]
So the first sed command works by inserting a blank table row above the first line with class="tdci", but the almost identical second sed command meant to insert a blank table row after the last line with class="tdci" does not work.
I usually save these kinds of edits, editing between multiple lines, for vim since I never have problems with its similar command, but for some reason sed's" N;s/ has always been hit and miss for me, as in this example, where one instance works fine, yet a second does not. The script removes all leading/trailing whitespace and any Winblowz carriage returns (\r) before these commands get run.
Since I have a large number of files to edit I would of course prefer to get this working in a script if anyone might be able to see anything obvious I am doing wrong.
Additional details:
Sorry, I forgot to mention that I am running sed in Linux (Debian stable)


Check Solution