【发布时间】:2018-04-06 08:27:22
【问题描述】:
我想更改多个 mercurial hgrc 文件。
起始文件(示例):
# example repository config (see "hg help config" for more info)
[paths]
default = http://ourmercurialserver.de/apps/
# path aliases to other clones of this repo in URLs or filesystem paths
...
想要的结果
# example repository config (see "hg help config" for more info)
[paths]
default = ssh://myusername@ourmercurialserver.de//path/to/repositories/apps/
default-push = http://ourmercurialserver.de/apps/
# path aliases to other clones of this repo in URLs or filesystem paths
...
我创建了几个命令。但是第一个在 Mac 上不起作用
-
插入第二行 default-push 并找到表达式 default = http(*):
find . -type f -name 'hgrc' -exec sed -i '' s/((^)default = http(.*)($))/\1\n\default-push = \1/ {} +错误:-bash: 意外标记 `(' 附近的语法错误 (使用 (^) 和 ($) 和不使用时会发生错误)
-
将 default-push = default = http 替换为 default-push = http:
find . -type f -name 'hgrc' -exec sed -i '' s/((^)default-push = default = http)/default-push = http/ {} + -
有效 :-) 将默认的 http-String 替换为 ssh-String:
find . -type f -name 'hgrc' -exec sed -i '' s/default = http:\\/\\/ourmercurialserver.de/ssh:\\/\\/default = myusername@ourmercurialserver.de\\/\\/path\\/to\\/repositories/ {} +
编辑:我的最终解决方案(感谢 Torek 和 meistermuh)
-
插入第二行默认推送
find . -type f -name 'hgrc*' -exec sed -i '' -e 's,^default = http://\(.*\),default = http://nkoch@\1\ default-push = http://\1,' {} + -
将默认的 http-String 替换为 ssh-String
find . -type f -name 'hgrc' -exec sed -i '' 's,^default = http://myusername@ourmercurialserver.de,default = ssh://myusername@ourmercurialserver.de//path/to/repositories,' {} +
【问题讨论】:
-
(^) 和 ($) 可能是个问题。只需删除括号,因为您不需要并且实际上也不能对 0 字节信号进行分组(^ 和 $ 实际上不是字符等,而是一种用于“行首”和“行尾”的信号)跨度>
-
感谢您的快速回复。 (^) 和 ($) 是否存在并不重要。我总是收到错误“-bash: 意外标记 `('”附近的语法错误将在原始帖子中阐明
-
啊,好的,我明白了。将您的替换放在单引号中,并且仅在需要时使用圆括号对模式进行分组。示例:
find . -type f -name 'hgrc' -exec sed -i '' 's/^default-push = default = http)/default-push = http/' {} +在您的第一个示例中,\n也会导致问题,因为文件名中不允许换行。顺便说一句:我目前不知道为什么你的第三个正在工作,因为你替换之前的空单引号(在每个示例中)接缝对我来说是错位的 -
在尝试之前最重要的是:复制文件以进行备份;)
标签: regex http ssh find mercurial