Find text and replace with index

1

I need to find a string and replace it with a moving index (in this case find pattern is "replaceThis")

For example:

id=replaceThis
......
id=replaceThis
......
id=replaceThis

should become

id=0
......
id=1
......
id=2

My environment is windows (and notepad++) but I also have access to cygwin

aiao

Posted 2015-03-08T14:49:53.197

Reputation: 193

See [so] question Notepad++ incrementally replace

– DavidPostill – 2015-03-08T14:51:45.703

Answers

2

It's fairly easy in awk (in Cygwin):

awk '{ while ($0 ~ /replaceThis/) sub(/replaceThis/, counter++) } 1'

On each line, for as long as it contains the string you're looking for, replace (one occurrence of) it with the value of the counter, which you then increment.  The 1 at the end is a shorthand way of telling awk to print the line after it has replaced all occurrences (if any) of the string; you could just as well say

awk '{ while ($0 ~ /replaceThis/) sub(/replaceThis/, counter++); print }'

Scott

Posted 2015-03-08T14:49:53.197

Reputation: 17 653

looks promising... will try and report back – aiao – 2015-03-08T15:27:43.970

works like a charm. Awk is quite cumbersome in syntax and usage. Any pointers to start learning? – aiao – 2015-03-08T16:39:14.350