Site Overlay

How to find and replace, incrementing the output by one each time

Recently had to fix a WordPress database with entries having a duplicate key.

They were 1974 entries with the same key.

And to fix that, as suggested by every resource online, you need to increment each key by one.

Good luck doing that manually.

Instead, in Ubuntu that can be done in a matter of seconds with Perl.

Use the following command:

perl -pi.bak -pe 'BEGIN{$A=STARTING_VALUE_INTEGER;} s/KEY_TO_REPLACE/$A++/ge' yourFile.sql

That’s it.

-p processes and prints <> line by line.

-i activates in-place editing. Files are backed up using the .bak extension.

Leave a Reply

Your email address will not be published. Required fields are marked *