【发布时间】:2011-02-11 09:37:40
【问题描述】:
我有一个降价文件,其中包含 [this][]、[that][]、... 和 [the other][] 等词。我知道如何在 MacVim 中找到这些词,但是如何将它们替换为 [this][1]、[that][2]、... 和 [the other][n],其中 n 在我的案例?
如果证明比使用 MacVim 更简单,我也会接受使用 sed 或 awk 甚至 Ruby 的解决方案。
【问题讨论】:
我有一个降价文件,其中包含 [this][]、[that][]、... 和 [the other][] 等词。我知道如何在 MacVim 中找到这些词,但是如何将它们替换为 [this][1]、[that][2]、... 和 [the other][n],其中 n 在我的案例?
如果证明比使用 MacVim 更简单,我也会接受使用 sed 或 awk 甚至 Ruby 的解决方案。
【问题讨论】:
perl -p -i -e 's/(\[.*?\])\[\]/"$1\[".(++$i)."]"/ge' /path/to/file
维姆:
:let g:lastcount=0
:function PlusPlus()
let g:lastcount+=1
return g:lastcount
endfunction
:%g/./s/\V[\.\{-}][\zs\ze]/\=PlusPlus()/g
【讨论】:
ruby -p -e \
'begin t=$_.clone; $_.sub! "][]", "][#{@n=@n.to_i+1}]";end until t==$_' \
< somefile
或者,对于就地编辑文件版本:
ruby -i.tmp -p -e \
'begin t = $_.clone; $_.sub! "][]", "][#{@n=@n.to_i+1}]"; end until t == $_' \
somefile
【讨论】:
嗯,在 Vim 中编写解决方案是很有可能的。我一直在使用这个 Incrementor 对象来处理这些事情:
---8
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
将整个代码示例复制到文件末尾的“栏”,然后保存并获取它 (:so %) 以在 Vim 中进行测试。
【讨论】:
如果您只需要执行一次,并且不再需要,那么在编辑器中执行就可以了。当您必须重复执行此操作时,手动执行会变得很痛苦,这时就需要自动化。
如果没有包含目标的文本样本,这有点像在黑暗中射击,但这似乎接近您使用 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.
【讨论】:
试试这个:
awk 'BEGIN{c=1}{for(w=1;w<=NF;w++){s=sub("\\[\\]","["c"]",$w);if(s)c++};print}' inputfile
【讨论】:
只需使用几个 vim 命令和一个宏,您就可以很容易地做到这一点:
/\[\]<cr>a1<esc>qqyi[np<C-a>q25@q
即搜索字符串“[]”,为第一个追加数字1。然后开始录制宏。抽出 [] 中的所有内容,转到下一个匹配项,然后将其粘贴。然后增加数字。停止录制,然后根据需要多次重播宏。它会增加它每次插入的数字。
【讨论】: