I would like to find every occurrence of a string in a large body of text and replace the nth occurrence of that string with the nth element in an array of replacement strings.
I have a large text file of XML with the url/path of a particular image. This url occurs 1000 times in this file. I have an array of 1000 unique image paths that I would like to substitute into this text file.
The basic idea is: needle: IM_5sWQ4n0fUWh0jVH haystack: random XML..src=IM_5sWQ4n0fUWh0jVH...random XML... src=IM_5sWQ4n0fUWh0jVH... random XML... src=IM_5sWQ4n0fUWh0jVH...
Array of image url paths: replaceArray = {IM_5sWQ4n0fUWh0jVH, IM_31DS439u38, IM_8939cSd9321,...}
Goal: Replace first occurrence of IM_5sWQ4n0fUWh0jVH with the first element of replaceArray, replace the second occurrence of IM_5sWQ4n0fUWh0jVH with the second element of replaceArray, etc.
Desired output:
random XML..src=IM_5sWQ4n0fUWh0jVH...random XML... src=IM_31DS439u38... random XML... src=IM_8939cSd9321...
Does anyone have any idea how to go about doing this preferably in R? I've looked around the web a bit but haven't found the answer so far. Thanks in advance!