Vim/sed/awk 查找&替换为增量整数
我有一个 markdown 文件,其中包含 [this][]、[that][]、... 和 [the other][] 等单词。我知道如何在 MacVim 中找到这些单词,但是如何用 [this][1]、[that][2]、... 和 [the other][n] 替换它们,其中 n 在我的中为 26案件?
我也会接受使用 sed 或 awk 甚至 Ruby 的解决方案,如果它们被证明比使用 MacVim 更简单的话。
I have a markdown file with words like [this][], [that][], ... , and [the other][]. I know how to find these words in MacVim, but how do I replace them with [this][1], [that][2], ..., and [the other][n], where n is 26 in my case?
I'll also accept solutions using sed or awk or even Ruby if they prove to be simpler than using MacVim.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
维姆:
Vim:
或者,对于就地编辑文件版本:
Or, for the edit-the-file-in-place version:
嗯,在 Vim 中编写一个解决方案是完全可能的。我已经使用这个 Incrementor 对象有一段时间了:
---8<--- vim code
将整个代码示例复制到文件末尾的“栏”,然后保存它并获取它 (:so %) 以在 Vim 中进行测试。
Well, writing a solution to this in Vim is quite possible. I have been using this Incrementor object for a while now for these sort of things:
---8<--- vim code
Copy that whole code sample right up to the 'bar' at the end in a file and then save it and source it (:so %) to test from within Vim.
如果您只需要执行一次,并且不再需要执行此操作,那么在编辑器中执行就可以了。当您必须重复执行此操作时,手动执行此操作将变得非常痛苦,这就是需要启动自动化的时候。
如果没有包含目标的文本样本,这有点像在黑暗中射击,但这似乎接近于您使用 Ruby 的描述:
If you only have to do it once, and never again, then doing it in an editor is fine. When you have to do it repeatedly then it becomes a major pain to do it manually, and that's when automation needs to kick in.
Without a sample of the text containing the targets it is somewhat like shooting in the dark, however this seems close to your description using Ruby:
尝试一下:
Give this a try:
只需使用几个 vim 命令和一个宏,您就可以轻松完成此操作:
即搜索字符串“[]”,为第一个字符串附加数字 1。然后开始录制宏。猛拉 [] 内的所有内容,转到下一个匹配项,然后粘贴它。然后增加数字。停止录制,然后根据需要多次重播宏。它会增加每次插入的数字。
You can do this pretty easily just by using a few vim commands, and a macro:
That is, search for the string "[]", append the number 1 for the first one. Then start recording a macro. Yank everything inside the [], go to the next match, and paste it. Then increment the number. Stop recording and then replay the macro as many times as you need to. It will increment the number it inserts each time.