My awk script reads records from file1, finds the same records in file2 and substitutes alternate positions (of the record) with a defined symbol in that. But few of the values are not getting substituted as desired. Only the first instance of the word is getting substituted, eg
TYTYTYintoT##Y##TYTYTYGGHGintoT##Y##GGHGLEFTintoL##TLEFT NAMEintoL##T NAME
Expected output is given below.
My code-
awk ' FNR==NR
{
if ($0 in word)
next
word[$0]=$0
for (i=1;i<=NF;i++)
{
old=$i
new=""
while (old) {
len=length(old)
new=new substr(old,1,1) substr("##",1,len-1)
old=substr(old,4)
}
id=index(word[$0],$i)
word[$0]=substr(word[$0],1,id-1) new substr(word[$0],id+length($i))
}
next
}
{
for (i in word)
{
regex = "\\<" i "\\>"
gsub(regex, ext[i])
#id=index($0,i)
#while(id>0) {
#$0=substr($0,1,id-1) word[i] substr($0,id+length(word[i]))
#id=index($0,i)
}
}
print
}' records test.html > output.html
$ cat records
LEFT NAME
LEFT NAME 2
LEFT
LEFT 123
TYTYTYGGHG
TYTYTY
AB 8263
AB SCENARIO DEBUG
AB 8263 SCENARIO DEBUG
$ cat test.html
<html>
<body>
<hr><br><>span class="table">TabA</span><table>
<tr class="column">
<td>LEFT NAME</td>
<td>LEFT</td>
<td></td>
<td>LEFT NAME 2</td>
<td>LEFT 123</td>
<td>TYTYTYGGHG</td>
<td></td>
<td>TYTYTY</td>
</tr>
<tr class="data">
<td></td>
<td></td>
<td></td>
<td>AB 8263</td>
<td></td>
<td></td>
<td>AB SCENARIO DEBUG</td>
<td>AB 8263 SCENARIO DEBUG</td>
</tr>
</table>
</body>
</html>
desired op -
<html>
<body>
<hr><br><>span class="table">TabA</span><table>
<tr class="column">
<td>L##T N##E</td>
<td>L##T</td>
<td></td>
<td>L##T N##E 2</td>
<td>L##T 1##</td>
<td>T##Y##G##G</td>
<td></td>
<td>T##Y##</td>
</tr>
<tr class="data">
<td></td>
<td></td>
<td></td>
<td>A# 8##3</td>
<td></td>
<td></td>
<td>A# S##N##I# D##U#</td>
<td>A# 8##3 S##N##I# D##U#</td>
</tr>
</table>
</body>
</html>
Current output -
<html>
<body>
<hr><br><>span class="table">TabA</span><table>
<tr class="column">
<td>L##T NAME</td>
<td>L##T</td>
<td></td>
<td>L##T NAME 2</td>
<td>L##T 123</td>
<td>T##Y##GGHG</td>
<td></td>
<td>T##Y##</td>
</tr>
<tr class="data">
<td></td>
<td></td>
<td></td>
<td>A# 8##3</td>
<td></td>
<td></td>
<td>A# S##N##I# D##U#</td>
<td>A# 8##3 SCENARIO DEBUG</td>
</tr>
</table>
</body>
</html>