Reputation: 781
I have a very large text file that I have opened it in VIM and I need to add 1 to numbers matching some criteria. for example:
Comment 1
Comment 2
[p2,0,0,115]Live! ConcertGrand
[p2,2,104,5]Live! PopGrand
[p2,3,104,4]Live! RockPiano
[p2,4,104,3]Live! AmbientPiano
End of file.
and I'd like to transform this (by adding say 1 to the second element of the list) to
Comment 1
Comment 2
[p2,1,0,115]Live! ConcertGrand
[p2,3,104,5]Live! PopGrand
[p2,4,104,4]Live! RockPiano
[p2,5,104,3]Live! AmbientPiano
End of file.
How can I do this in vim, please?
I have tried:
%s/\[p\zs\d\+\(,\d\+\)\=/\=(1+str2nr(submatch(1)))/
But it does not work. Any help would be greatly appreciated. CS
Upvotes: 2
Views: 103
Reputation: 15390
Do you really need to do this with search and replace? Because there is builtin functionality for addition and subtraction.
:h CTRL-A
CTRL-A: Add [count] to the number or alphabetic character at or after the cursor.
{Visual}CTRL-A: Add [count] to the number or alphabetic character in the highlighted text. {not in Vi}
So you basically could use VISUAL-BLOCK selection on your column of numbers and press CTRL-A
and it will add 1 to all of them
If it is more complicated you could use macro
Upvotes: 3