This problem has me going around in circles. I will try to explain the problem
Suppose I have 2 files
A and B
I search for a pattern X in file A
First I want to extract the line numbers pattern X appears in file A.
Now suppose pattern X appears in line numbers 4, 10, 17, 19, 24.
Second step is to search for a pattern Y in file B.
And then I want to replace pattern Y in file B with pattern Z on the n'th occurance
This means that on the 4th, 10th, 17th, 19th and 24th occurance of pattern Y in file B I should replace pattern Y with pattern Z in file B.
I hope I have been able to explain the problem properly
Thanks