我有一个非常大的文本文件,我已在 VIM 中打开它,我需要将符合某些条件的数字加 1。例如:
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.
我想将其转换(通过将 1 添加到列表的第二个元素)到
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.
请问如何在 vim 中执行此操作?
我已经尝试过:
%s/\[p\zs\d\+\(,\d\+\)\=/\=(1+str2nr(submatch(1)))/
但是它不起作用。任何帮助将不胜感激。 CS
最佳答案
您真的需要通过搜索和替换来执行此操作吗?因为有内置的加法和减法功能。
: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}
因此,您基本上可以在数字列上使用 VISUAL-BLOCK 选择,然后按 CTRL-A
,它会将所有数字加 1
如果更复杂,你可以使用宏
关于regex - 向符合某些条件的数字添加常量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44061943/