Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex for Replacing String with Incrementing Number

Once upon a time I used UltraEdit for making this

text 1
text 2
text 3
text 4
...

from that

text REPLACE
text REPLACE
text REPLACE
text REPLACE
...

it was as easy as replace REPLACE with \i

now, how can I do this with eg. sed?

If you provide a solution, could you please add directions for filling the result with leading zeros?

thanks

like image 583
JPT Avatar asked Feb 17 '26 02:02

JPT


1 Answers

You can use awk instead of sed for this:

awk '{ sub(/REPLACE/, ++i) } 1' file
text 1
text 2
text 3
text 4
...

Code Demo

like image 60
anubhava Avatar answered Feb 19 '26 20:02

anubhava