【问题标题】:Replace a line containing certain characters using vi使用 vi 替换包含某些字符的行
【发布时间】:2015-11-22 14:09:12
【问题描述】:

我想将所有包含“CreateTime=xxxxx”的行替换为“CreateTime=2012-01-04 00:00”。我可以知道我应该如何使用 vim 吗?

[m18]
Attendees=38230,92242,97553
Duration=2
CreateTime=2012-01-09 22:00

[m20]
Attendees=52000,50521,34025
Duration=2
CreateTime=2012-01-09 00:00

[m22]
Attendees=95892,23689
Duration=2
CreateTime=2012-01-08 17:00

【问题讨论】:

标签: vim vi


【解决方案1】:

您可以为此使用全局替换运算符。

:%s/CreateTime=.*$/CreateTime=2012-01-04 00:00/g

您可以在 Vim 中使用以下命令阅读 s 命令的帮助:

:help :s

您可以通过 :help pattern-overview 阅读有关模式的信息。

根据要求,关于正则表达式匹配的更多信息 (CreateTime=.*$):

CreateTime=  # this part is just a string
.            # "." matches any character
*            # "*" modifies the "." to mean "0 or more" of any character
$            # "$" means "end of line"

综合起来,它匹配 CreateTime= 后跟任意一系列字符,占用该行的其余部分。

【讨论】:

  • 你能解释一下这部分吗:.*$
  • @SagarJain 我用更多信息编辑了答案。
猜你喜欢
  • 1970-01-01
  • 2017-06-25
  • 1970-01-01
  • 1970-01-01
  • 2019-12-04
  • 2013-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多