Sublime Forum

Search and repalce

#1

I have a daily process that I do. Sometimes multiple times per day. I am both new to Sublime and Python so I am looking for a bit of direction.

The file that I have could have any number of lines and each line will have a value key:xxxxxxxxx; I need to iterate over the line and replace each value and increment by one but only if the line starts with a special keyword. I would also need to supply a start value

Given the following:

special adkey:222; key:111111111;

special adkey:222; key:111111111;

special adkey:222; key:111111111;

special adkey:222; key:111111111;

After running the python script with a start value of 214103101 I would like the file to look like:

special adkey:222; key:214103101;

special adkey:222; key:111111111;

special adkey:222; key:111111111;

special adkey:222; key:214103102;

Now I can use a regular expression to match what I need easy enough:
^special
(?<!ad)key\x3a\d+\x3b

So in a nutshell:

foreach(lines):
if (line == ^special):
replace (?<!ad)key\x3a\d+\x3b with key:i;
i++

Any help would be greatly appreciated.

0 Likes

#2

You could process the file with a python script, bash script or whatever you’re comfy with.

But I guess you’re looking forward to create your own plugin that takes the initial number loops over the lines in your file. The Sublime 3 API is described https://www.sublimetext.com/docs/3/api_reference.html and you can find a simple project https://github.com/jbrooksuk/InsertNums

0 Likes