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
To Remove the lines from the source file itself, use the -i option with sed command. If you dont wish to delete the lines from the original source file you can redirect the output of the sed command to another file.
sed + replace only the second match word.
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}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With