I have big XML file
<obj param="2542">
<obj param="2333">
<obj param="6433">
I need to increase all "param" values by some number. I can match numbers that I need with regexp search in many editors, but how to apply some math to the replacement?
|
I have big XML file
I need to increase all "param" values by some number. I can match numbers that I need with regexp search in many editors, but how to apply some math to the replacement?
| ||||
|
feedback
|
|
After a bit more poking around, it turns out vim can do it with a single command, without scripting. For example, to add 50 to all numbers following
Let me break that down.
Everything up to the next occurrence of the delimiter @ is the reg ex pattern to search for, in this case Then the delimiter The Then we have
Hence The delimiter The flag You can probably work out from there how to add different numbers, or subtract, or divide, etc. | ||||
|
feedback
|
|
In Emacs (since version 23): use
In Vim: start your replacement text with
Several editors allow you to do this kind of things with macros: define a macro that 1. looks for the next match and 2. performs the replacement (using an external tool for arithmetic if necessary); repeat the macro as many times as you have matches. | |||
|
feedback
|
|
You can use vim to do that for you. Just open your file and record a macro. Example: Search for any number
then press q and a (store macro in register a). After that, press Ctrl-X (increasing the number by 1) and press n (for next search result) afterwards. After you have done that, press q again to save the macro. Now you can apply the macro to the next number by pressing @+a. This will change the current number and jumps to the next one. By repeating that or using x@a, you can repeat that x times. Well, that description may not be sufficient to show how it can be done. Just refer to a tutorial describing the macro mechanism in vim. | |||||||
feedback
|
|
The emacs editor could do it - or you could do it with a very simple python program. | |||
|
feedback
|
vimandemacscan do this kind of thing, but it might require writing a script rather than using a single command. – frabjous Sep 27 '10 at 18:20