Not everything needs to be done in pure elisp or using just Emacs facilities. In this case, I would use shell-command-on-region
(usually bound to M-|): first mark the region of interest and then type M-| and pass the following shell command to it (followed by RET):
awk -F ":" '/finished/ {n = $2+1; printf "%s: %s,\n", $1, n; next;} {print $0;}'
In words: use a colon as a field separator, for each line, if the line matches finished
, increment the second field and print out the modified line, then go immediately to the next line; otherwise, just print the line. There are probably shorter ways to do it, and perhaps some error checking needs to be added (e.g. if the second field on finished
lines is missing or not a number), but in the case you showed, this should be enough.
Running shell-command-on-region
with that command will produce output in a separate buffer, where you can check and make sure that it did the right thing. Once you are sure it is doing the right thing, you can replace the original region with the modified text using a C-u prefix on shell-command-on-region
:
C-u M-| awk -F ":" '/finished/ {n = $2+1; printf "%s: %s,\n", $1, n; next;} {print $0;}' RET
shell-command-on-region
allows you to use all sorts of tools outside Emacs to do text processing, so IMO it is an indispensable tool in one's tool box.