我有一個標記文件,其中包含[this] [],[that] [],...和[other] []等詞。我知道如何在MacVim中找到這些單詞,但是如何將它們替換爲[this] [1],[that] [2],...和[other] [n],其中n是26案件?Vim/sed/awk使用增量整數查找並替換
如果證明比使用MacVim更簡單,我還會接受使用sed或awk甚至Ruby的解決方案。
我有一個標記文件,其中包含[this] [],[that] [],...和[other] []等詞。我知道如何在MacVim中找到這些單詞,但是如何將它們替換爲[this] [1],[that] [2],...和[other] [n],其中n是26案件?Vim/sed/awk使用增量整數查找並替換
如果證明比使用MacVim更簡單,我還會接受使用sed或awk甚至Ruby的解決方案。
ruby -p -e \
'begin t=$_.clone; $_.sub! "][]", "][#{@[email protected]_i+1}]";end until t==$_' \
< somefile
或者,在編輯的文件就地版本:
ruby -i.tmp -p -e \
'begin t = $_.clone; $_.sub! "][]", "][#{@[email protected]_i+1}]"; end until t == $_' \
somefile
如果你只有做一次,也再沒有,然後做它在編輯器中是罰款。當你不得不重複這麼做的時候,手動完成它就成爲一大難題,那就是自動化需要啓動的時候。
沒有包含目標文本的樣本,它有點像在黑暗中拍攝,但是這似乎用接近你的描述紅寶石:
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
perl -p -i -e 's/(\[.*?\])\[\]/"$1\[".(++$i)."]"/ge' /path/to/file
的Vim:
:let g:lastcount=0
:function PlusPlus()
let g:lastcount+=1
return g:lastcount
endfunction
:%g/./s/\V[\.\{-}][\zs\ze]/\=PlusPlus()/g
那麼,在Vim中編寫一個解決方案是很有可能的。我一直用這個增量器對象,而現在,這些樣的東西:
--- 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
複製整個代碼示例說得最多的「酒吧」最後保存在一個文件中,然後保存並將其源代碼(:so%)從Vim中進行測試。
您只需通過幾個vim的命令做到這一點很容易,和宏:
/\[\]<cr>a1<esc>qqyi[np<C-a>[email protected]
也就是說,搜索字符串「[]」,追加1號的第一個。然後開始錄製一個宏。將[]中的所有內容都抽出,然後進入下一個比賽,然後粘貼。然後增加數字。停止錄製,然後根據需要多次重播該宏。它會增加它每次插入的數字。
如果你想自動化你在vim中做的事情,你只需要編寫一個映射(或者用於更復雜任務的腳本)。這裏沒有痛苦,這就是爲什麼很多程序員喜歡vim。 – ZyX 2011-01-29 23:16:18
順便說一下,如果你在vim中這樣做,你將能夠輕鬆地撤消一些事情(即使你已經退出了編輯器)。 – ZyX 2011-01-29 23:20:19
我住在vim/gvim/macvim中,在工作和家中進行了99%的編輯,但我仍然沒有嘗試將它用作自動化工具。 – 2011-01-30 03:35:10