【问题标题】:Vim/sed/awk Find & Replace with Incremental IntegerVim/sed/awk 用增量整数查找和替换
【发布时间】:2011-02-11 09:37:40
【问题描述】:

我有一个降价文件,其中包含 [this][]、[that][]、... 和 [the other][] 等词。我知道如何在 MacVim 中找到这些词,但是如何将它们替换为 [this][1]、[that][2]、... 和 [the other][n],其中 n 在我的案例?

如果证明比使用 MacVim 更简单,我也会接受使用 sed 或 awk 甚至 Ruby 的解决方案。

【问题讨论】:

    标签: ruby vim sed awk macvim


    【解决方案1】:
    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
    

    【讨论】:

      【解决方案2】:
      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
      

      【讨论】:

        【解决方案3】:

        嗯,在 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 中进行测试。

        【讨论】:

          【解决方案4】:

          如果您只需要执行一次,并且不再需要,那么在编辑器中执行就可以了。当您必须重复执行此操作时,手动执行会变得很痛苦,这时就需要自动化。

          如果没有包含目标的文本样本,这有点像在黑暗中射击,但这似乎接近您使用 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.
          

          【讨论】:

          • 如果您想自动化您在 vim 中所做的事情,您只需编写一个映射(或用于更复杂任务的脚本)。这里没有痛苦,这就是为什么许多程序员喜欢 vim。
          • 顺便说一句,如果你在 vim 中这样做,你将能够轻松地撤消操作(使用持久撤消:即使你已经退出了编辑器)。
          • 我在工作和家庭中 99% 的编辑工作都使用 vim/gvim/macvim,但我仍然不会尝试将其用作自动化工具。
          【解决方案5】:

          试试这个:

          awk 'BEGIN{c=1}{for(w=1;w<=NF;w++){s=sub("\\[\\]","["c"]",$w);if(s)c++};print}' inputfile
          

          【讨论】:

          • 小建议:awk 'BEGIN {c=1} {for(w=1;w
          • @Beta:谢谢。我忽略了这一点。
          【解决方案6】:

          只需使用几个 vim 命令和一个宏,您就可以很容易地做到这一点:

          /\[\]<cr>a1<esc>qqyi[np<C-a>q25@q
          

          即搜索字符串“[]”,为第一个追加数字1。然后开始录制宏。抽出 [] 中的所有内容,转到下一个匹配项,然后将其粘贴。然后增加数字。停止录制,然后根据需要多次重播宏。它会增加它每次插入的数字。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2020-02-03
            • 2017-11-11
            • 2012-12-30
            • 2014-12-04
            • 1970-01-01
            • 1970-01-01
            • 2019-07-20
            • 2017-02-14
            相关资源
            最近更新 更多