Replacing alternate line in vi text file with a particular string

隐身守侯 提交于 2019-12-24 16:19:06

问题


I have text file which consists of 5 columns , if i want to replace every alternate line in second column with particular string ,how can do this in vi text editor?

eg: 1 CG 54 76 76
    2 CG 45  78 78
    3 CG 48  76 89
    4 CG 49  50 52

if i want to replace 2 nd and 4 th line (2nd column) with CA how can i do this in vi text editor?


回答1:


If you're using vim, this is a simple macro. If you're not sure if you're using vim, then in command mode, type qq and check that you see recording in the status line at the bottom. If so, then you can use this macro.

Beginning on line 1, record the macro with: qqj0wcwCA[Esc]jq

This will edit the 2nd line and leave you on the 3rd line, and now you have the macro in register q. Type 100@q to execute the command in register q 100 times.

Command details:

  • Begin on Line 1
  • qq - this begins recording a macro, you'll see recording in the status line at the bottom.
  • j - moves to the next (alternate) line.
  • 0 - moves to the beginning of the line.
  • w - jumps to the next word (2nd column)
  • cwCA - changes this word to CA.
  • Esc key - leave edit mode.
  • j - moves to the next line.
  • q - this stops recording the macro.


来源:https://stackoverflow.com/questions/10680552/replacing-alternate-line-in-vi-text-file-with-a-particular-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!