A.Grandt
A.Grandt

Reputation: 2262

regex search with renumbering in replace

I have a file with anywhere between a dozen and hundreds of matches on the search

 /playOrder="(\d+)"/ 

These are in the index file of an ePub ebook, in case anyone is wondering.

Is it possible to have a perl regex replace what finds all these, and "magically" renumber them all to a sequence, starting from 1?

Upvotes: 1

Views: 481

Answers (1)

TLP
TLP

Reputation: 67910

Posting comment as answer, as requested by OP:

perl -pe 's/playOrder="\K\d+"/++$i . q(")/ge' infile > outfile 

This one-liner is using a replacement field which is created by evaluation, creating a sequence like 1", 2"...

Further optimization can be made if using a lookahead assertion instead of inserting a new double quote ":

perl -pe 's/playOrder="\K\d+(?=")/++$i/ge' infile > outfile 

Upvotes: 3

Related Questions