【问题标题】:How do I add a prefix to my vi editor in consecutive lines ?如何在连续行中向我的 vi 编辑器添加前缀?
【发布时间】:2018-10-21 08:25:39
【问题描述】:

我的输入:

Test1.txt 
a
a
b
b
c
c

预期结果应该是

chown 用户:用户 a

chmod 755 a

chown 用户:用户 b

chmod 755 b

chown 用户:用户 c

chmod 755 c

请建议在单个文档中处理 10k + 行的最佳方法。 基本上为同一路径添加2个不同的前缀命令(一个接一个地重复两次)

提前致谢

【问题讨论】:

  • Vim visual block mode 可能你可以考虑。因为你在不同的行有不同的前缀,所以你必须用sed处理每一行。
  • Stack Overflow 是一个编程和开发问题的网站。这个问题似乎离题了,因为它与编程或开发无关。请参阅帮助中心的What topics can I ask about here。也许Super UserUnix & Linux Stack Exchange 会是一个更好的提问地点。
  • 如果你需要一个vi命令,为什么要使用sed标签?

标签: linux sed editor vi


【解决方案1】:

如果你的文件是这样的

a
a
b
b
c
c

你可以使用 vi 命令

:%s/^\(.*\)\n\(.*\)/chown user:\1\rchmod 755 \2/g

如果整个文件遵循该格式。

使用分组。 \n 匹配换行符,\r 插入换行符。

或者如果包含文件名并且你的文件是这样的

Test1.txt 
a
a
b
b
c
c

使用

:2,$s/^\(.*\)\n\(.*\)/chown user:\1\rchmod 755 \2/g

【讨论】:

  • 非常感谢您的回答。
【解决方案2】:

使用唯一路径列表更容易,因此首先删除每隔一行:

:g/^/+d

(取自this answer

然后用你想要的命令替换每一行:

:%s/.*/chown user:user &\rchmod 755 &/

:%                                     run this command on the entire buffer:
  s                                    replace
   /.*/                                an entire line with
       chown user:user                 (literal "chown user:user ")
                       &               the entirety of the match
                        \r             newline
                          chmod 755    (literal "chmod 755 ")
                                    &  the entirety of the match again
                                     / (end the regex)

&\r 记录在 :help sub-replace-special 中。)

【讨论】:

  • 感谢 AuxTaco 的回答
猜你喜欢
  • 1970-01-01
  • 2015-03-29
  • 2010-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-04
  • 1970-01-01
相关资源
最近更新 更多