I have a html file that has multiple nested tables:
<table>
<tr>
<td>
<table>
<tr>
<td>
<table>
...
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
I would like to add classes to each of the tables as such:
<table class="table1">
<tr>
<td>
<table class="table2">
<tr>
<td>
<table class="table3">
...
</table>
</td>
</tr>
</table>
Based on extensive searches, I cobbled the following bash script, but it's not working at all:
#!/bin/bash
strng="<table"
index=1
for entry in `grep -n $strng $1`
do
line=`echo $entry | awk -F":" '{print$1}'`
sed -e "$line s/$strng/$strng class=\"table$index\"/" -i $1
index=$(($index + 1))
done
Any recommendations will be appreciated.
awkis the more suitable tool for this task. Usingbashloop for text processing is the bad practice. Read here: Why is using a shell loop to process text considered bad practice?