用特定字符串替换 vi 文本文件中的备用行 [英] Replacing alternate line in vi text file with a particular string

查看:33
本文介绍了用特定字符串替换 vi 文本文件中的备用行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由 5 列组成的文本文件,如果我想用特定的字符串替换第二列中的每一行,如何在 vi 文本编辑器中执行此操作?

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

如果我想用 CA 替换第 2 行和第 4 行(第 2 列),我该如何在 vi 文本编辑器中执行此操作?

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

推荐答案

如果您使用 vim,这是一个简单的宏.如果您不确定是否使用 vim,则在命令模式下,键入 qq 并检查您是否在底部的状态行中看到 recording.如果是这样,那么您可以使用此宏.

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.

从第 1 行开始,记录宏:qqj0wcwCA[Esc]jq

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

这将编辑第 2 行并将您留在第 3 行,现在您在寄存器 q 中拥有宏.输入 100@q 执行寄存器 q 中的命令 100 次.

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.

命令详情:

  • 从第 1 行开始
  • qq - 这将开始录制宏,您将在底部的状态行中看到正在录制.
  • j - 移动到下一行(替代).
  • 0 - 移动到行首.
  • w - 跳转到下一个单词(第 2 列)
  • cwCA - 将此词更改为 CA.
  • Esc 键 - 退出编辑模式.
  • j - 移动到下一行.
  • q - 这将停止录制宏.
  • 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.

这篇关于用特定字符串替换 vi 文本文件中的备用行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆