Issue
I am trying to use sed to replace every other occurrence of an html element of a file so I can make alternating color rows.
Here is what I have tried and it doesn't work.
sed 's/<tr valign=top>/<tr valign=top bgcolor='#E0E0E0'>/2' untitled.html
Solution
I'd solve it with awk:
awk '/<tr valign=top>/&&v++%2{sub(/<tr valign=top>/, "<tr valign=top bgcolor='#E0E0E0'>")}{print}' untitled.html
First, it verifies if the line contains <tr valign=top>
/<tr valign=top>/&&v++%2
and whether the <tr valign=top>
is an odd found instance:
v++%2
If so, it replaces the <tr valign=top>
in the line
{sub(/<tr valign=top>/, "<tr valign=top bgcolor='#E0E0E0'>")}
Since all lines are to be printed, there is a block that always will be executed (for all lines) and will print the current line:
{print}
Answered By - brandizzi