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



Question:

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!


Solution:1

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+ \#)


Solution:2

JEdit can probably help you: http://www.jedit.org/

you can do all kinds of regex and even bean result based replacing with it.


Solution:3

UltraEdit32 is great and I believe it has the features you need. There is a free 30-day download so you can make sure. :)


Solution:4

I know you want an app available on Windows/Linux, but there's another solution on Mac : TextWrangler, and it's free.


Solution:5

Take a look at UltraEdit32. It's very good. Not free, but available in Windows, Linux and Mac platforms. It has regex based search & replace.


Solution:6

This script should let you do what you want in Vim.


Solution:7

Vim functions can do the incrementing number trick and aren't too hard to write. For example the Vim wiki says how to do this. See also :h sub-replace-\=.

function! Counter()      let i = g:c      let g:c = g:c + 1      return i  endfunction    :let c=1|%s/<\w\+\zs/\=' id="' . Counter() . '"'/g  

We've probably left user-friendliness long behind at this point but Vim's Ruby support can do this kind of thing easily too:

:ruby c=0  :rubydo $_.gsub!(/<\w+/){|m| c += 1; m + ' id="' + c.to_s + '"'}  

Or Perl:

:perl $c=1  :perldo s/<\w+/$& . ' id="' . $c++ . '"'/eg  


Solution:8

To me, this sounds like it might be a job for awk, rather than a job for an editor.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »