replacing text in a file, but...

Hi all,

Very first post on this forums, hope you can help me with this scripting task.

I have a big text file with over 3000 lines, some of those lines contain some text that I need to replace, lets say for simplicity the text to be replaced in those lines is "aaa" and I need it to replace it for something else...
but before you think of sed's substitution 's///' or python's s.replace() method or similar ways, the resulting text should NOT be the same for every occurrence of "aaa", that is, I need the replacements to be unique (this does not happens with the above functions as all of the occurrences are replaced with the same text.

For example, for the first occurrence of "aaa" replace it with "aaa1", the second "aaa2" and so forth. I doesn't necessarily have to be sequential it just has to be unique.

Any input is appreciated,
Thank you.

$ print 'aaa
bbb
aaa
bbb
aaa'|perl -pe's/aaa/$&.++$x/e' 
aaa1
bbb
aaa2
bbb
aaa3

Or:

$ print 'aaa
bbb
aaa
bbb
aaa'|awk '(/aaa/&&$1=$1(++_))||1'
aaa1
bbb
aaa2
bbb
aaa3

works as it should. tyvm ---------------- Now playing: Connecting...FoxyTunes