Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I'm looking for an application/text editor that [closed]

can best help me systematically modify the "replace" field of a regex search as it encounters each match.

For example, I have an xml file that needs the phrase "id = $number" inserted at regular points in the text, and basically, $number++ each time the regex matches (id = 1, id = 2, etc) until the end of the file.

I know I could just write a bash/perl/python script or some such, but I'd like it to be at least moderately user-friendly so I could teach my intelligent (but less technically-inclined) workers how to use it and make their own modifications. Regexing is not a problem for them.

The closest I've come so far is Notepad++'s Column Editor and 'increase [number] by' function, but with this I have to write a separate regex to align everything, add the increments, and then write another to put it back. Unfortunately, I need to use this function on too many different types of files and 'replace's to make macros feasible.

Ideally, the program would also be available for both Windows & Linux (WINE is acceptable but native is much preferred), and have a 'VI/VIM input' option (if it's a text editor), but these are of secondary importance.

Of course, it'd be nice if there is an OSS solution, and I'd be glad to donate $20-$50 to the developer(s) if it provides the solution I'm looking for.


Apologies for the length, and thanks so much for your help!

like image 707
chameleon3 Avatar asked Dec 18 '22 08:12

chameleon3


1 Answers

emacs (version 22 and later) can do what you're looking for. See Steve Yegge's blog for a really interesting read about it. I think this should work:

M-x replace-regexp
  Replace regexp: insert pattern regexp here
  Replace regexp with: id = \#

\# is a special metacharacter that gets replaced by the total number of replacements that have occurred so far, starting from 0. If you want the list to start from 1 instead of 0, use the following replacement string:

id = \,(1+ \#)
like image 121
Adam Rosenfield Avatar answered Jan 12 '23 10:01

Adam Rosenfield