Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim/sed/awk Find & Replace with Incremental Integer

I have a markdown file with words like [this][], [that][], ... , and [the other][]. I know how to find these words in MacVim, but how do I replace them with [this][1], [that][2], ..., and [the other][n], where n is 26 in my case?

I'll also accept solutions using sed or awk or even Ruby if they prove to be simpler than using MacVim.

like image 615
ma11hew28 Avatar asked Dec 16 '22 17:12

ma11hew28


1 Answers

perl -p -i -e 's/(\[.*?\])\[\]/"$1\[".(++$i)."]"/ge' /path/to/file

Vim:

:let g:lastcount=0
:function PlusPlus()
  let g:lastcount+=1
  return g:lastcount
  endfunction
:%g/./s/\V[\.\{-}][\zs\ze]/\=PlusPlus()/g
like image 189
ZyX Avatar answered Jan 09 '23 00:01

ZyX