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?
我有一个降价文件,其中包含[this] [],[that] [],...和[其他] []等字样。我知道如何在MacVim中找到这些单词,但是如何用[this] [1],[that] [2],...和[其他] [n]替换它们,其中n是26案件?
I'll also accept solutions using sed or awk or even Ruby if they prove to be simpler than using MacVim.
如果它们被证明比使用MacVim更简单,我也会接受使用sed或awk甚至Ruby的解决方案。
6 个解决方案
#1
6
perl -p -i -e 's/(\[.*?\])\[\]/"$1\[".(++$i)."]"/ge' /path/to/file
Vim:
Vim的:
:let g:lastcount=0
:function PlusPlus()
let g:lastcount+=1
return g:lastcount
endfunction
:%g/./s/\V[\.\{-}][\zs\ze]/\=PlusPlus()/g
#2
2
ruby -p -e \
'begin t=$_.clone; $_.sub! "][]", "][#{@n=@n.to_i+1}]";end until t==$_' \
< somefile
Or, for the edit-the-file-in-place version:
或者,对于编辑文件到位版本:
ruby -i.tmp -p -e \
'begin t = $_.clone; $_.sub! "][]", "][#{@n=@n.to_i+1}]"; end until t == $_' \
somefile
#3
2
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:
那么,在Vim中为此编写解决方案是非常有可能的。我一直在使用这个Incrementor对象一段时间用于这些事情:
---8<--- vim code
--- 8 <--- vim代码
function! Incrementor(start, step)
let incrementor = {}
let incrementor.initial_value = a:start
let incrementor.value = incrementor.initial_value
let incrementor.step = a:step
function incrementor.val() dict
return self.value
endfunction
function incrementor.next() dict
let self.value += self.step
return self.value
endfunction
function incrementor.reset() dict
let self.value = self.initial_value
return self.value
endfunction
return incrementor
endfunction
" With the Incrementor function above saved in, say,
" ~/.vim/plugin/incrementor.vim, you can then create incrementors as you need
" them and use them in substitutions, like this:
let inc = Incrementor(0,1)
28,$s/\v\[(\w+)\]\[\]/\="[".submatch(1)."][".inc.next()."]"/
finish
" test case
foo
[this][]
[that][]
[theother][]
bar
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.
将整个代码示例直接复制到文件末尾的“bar”,然后保存并将其源(:so%)从Vim中进行测试。
#4
1
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:
如果没有包含目标的文本样本,它有点像在黑暗中拍摄,但这似乎与使用Ruby的描述接近:
text = %{
[Lorem][] ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut
labore [et][] dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco
laboris nisi [ut][] aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in
voluptate velit esse [cillum][] dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat
non proident, sunt in culpa qui [officia deserunt][] mollit anim id est laborum.
}
text.scan(/\[[^\]]+\]\[\]/).each_with_index{ |t, i| text[t] = t.sub('[]', "[#{1 + i}]") }
puts text
# >>
# >> [Lorem][1] ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut
# >> labore [et][2] dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco
# >> laboris nisi [ut][3] aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in
# >> voluptate velit esse [cillum][4] dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat
# >> non proident, sunt in culpa qui [officia deserunt][5] mollit anim id est laborum.
#5
1
Give this a try:
尝试一下:
awk 'BEGIN{c=1}{for(w=1;w<=NF;w++){s=sub("\\[\\]","["c"]",$w);if(s)c++};print}' inputfile
#6
0
You can do this pretty easily just by using a few vim commands, and a macro:
只需使用几个vim命令和一个宏,你就可以很容易地做到这一点:
/\[\]<cr>a1<esc>qqyi[np<C-a>q25@q
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.
也就是说,搜索字符串“[]”,为第一个附加数字1。然后开始录制宏。将所有内容放入[]内,转到下一场比赛,然后粘贴。然后递增数字。停止录制,然后根据需要重播宏。它会增加每次插入的数字。
#1
6
perl -p -i -e 's/(\[.*?\])\[\]/"$1\[".(++$i)."]"/ge' /path/to/file
Vim:
Vim的:
:let g:lastcount=0
:function PlusPlus()
let g:lastcount+=1
return g:lastcount
endfunction
:%g/./s/\V[\.\{-}][\zs\ze]/\=PlusPlus()/g
#2
2
ruby -p -e \
'begin t=$_.clone; $_.sub! "][]", "][#{@n=@n.to_i+1}]";end until t==$_' \
< somefile
Or, for the edit-the-file-in-place version:
或者,对于编辑文件到位版本:
ruby -i.tmp -p -e \
'begin t = $_.clone; $_.sub! "][]", "][#{@n=@n.to_i+1}]"; end until t == $_' \
somefile
#3
2
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:
那么,在Vim中为此编写解决方案是非常有可能的。我一直在使用这个Incrementor对象一段时间用于这些事情:
---8<--- vim code
--- 8 <--- vim代码
function! Incrementor(start, step)
let incrementor = {}
let incrementor.initial_value = a:start
let incrementor.value = incrementor.initial_value
let incrementor.step = a:step
function incrementor.val() dict
return self.value
endfunction
function incrementor.next() dict
let self.value += self.step
return self.value
endfunction
function incrementor.reset() dict
let self.value = self.initial_value
return self.value
endfunction
return incrementor
endfunction
" With the Incrementor function above saved in, say,
" ~/.vim/plugin/incrementor.vim, you can then create incrementors as you need
" them and use them in substitutions, like this:
let inc = Incrementor(0,1)
28,$s/\v\[(\w+)\]\[\]/\="[".submatch(1)."][".inc.next()."]"/
finish
" test case
foo
[this][]
[that][]
[theother][]
bar
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.
将整个代码示例直接复制到文件末尾的“bar”,然后保存并将其源(:so%)从Vim中进行测试。
#4
1
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:
如果没有包含目标的文本样本,它有点像在黑暗中拍摄,但这似乎与使用Ruby的描述接近:
text = %{
[Lorem][] ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut
labore [et][] dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco
laboris nisi [ut][] aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in
voluptate velit esse [cillum][] dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat
non proident, sunt in culpa qui [officia deserunt][] mollit anim id est laborum.
}
text.scan(/\[[^\]]+\]\[\]/).each_with_index{ |t, i| text[t] = t.sub('[]', "[#{1 + i}]") }
puts text
# >>
# >> [Lorem][1] ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut
# >> labore [et][2] dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco
# >> laboris nisi [ut][3] aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in
# >> voluptate velit esse [cillum][4] dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat
# >> non proident, sunt in culpa qui [officia deserunt][5] mollit anim id est laborum.
#5
1
Give this a try:
尝试一下:
awk 'BEGIN{c=1}{for(w=1;w<=NF;w++){s=sub("\\[\\]","["c"]",$w);if(s)c++};print}' inputfile
#6
0
You can do this pretty easily just by using a few vim commands, and a macro:
只需使用几个vim命令和一个宏,你就可以很容易地做到这一点:
/\[\]<cr>a1<esc>qqyi[np<C-a>q25@q
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.
也就是说,搜索字符串“[]”,为第一个附加数字1。然后开始录制宏。将所有内容放入[]内,转到下一场比赛,然后粘贴。然后递增数字。停止录制,然后根据需要重播宏。它会增加每次插入的数字。