【问题标题】:How to replace text in files with a text that has complicated characaters?如何用具有复杂字符的文本替换文件中的文本?
【发布时间】:2019-04-19 01:15:50
【问题描述】:

我需要运行下面的操作,但 bash 或 zsh 终端不喜欢这些字符。如何正确运行?

find ./ -type f -exec sed -i 's/PHRASE=TEST/pwds=`gpg --decrypt ~/test.gpg` & eval "$pwds"/gI' {} \;

PHRASE=TEST需要替换成pwds=gpg --decrypt ~/test.gpg& eval "$pwds"

我不需要使用 sed,我可以使用其他东西,只要它可以工作

【问题讨论】:

  • 一件事是替换文本的一部分 (~/text.gpg) 包含 / 命令的 / 分隔符。您可以转义它(~\/text.gpg)或使用不同的分隔符(例如s|...|...|gI)。不过,要获得更多帮助,请在您的预期输出中发布一行示例输入。
  • 这正是我想要做的事情,所以我需要确切的行。在我的示例中,什么被替换为什么。我需要提供什么示例行,不确定
  • 看我的帖子,我添加了我需要的东西。感谢您提供示例行,但它们不起作用。
  • 不以什么方式工作?你有错误吗?如果是这样,错误是什么?如果问题是输出不是您所期望的,则显示输入、您得到的输出以及所需的输出。

标签: text sed replace gnupg


【解决方案1】:

没有样本输入和预期输出,我无法验证 100%,但我很确定您只需要转义文件路径中的 /&

find ./ -type f -exec sed -i 's/PHRASE=TEST/pwds=`gpg --decrypt ~\/test.gpg` \& eval "$pwds"/gI' {} \;

或者,如果您愿意,可以通过更改 s 命令的分隔符来避免第一次转义:

find ./ -type f -exec sed -i 's|PHRASE=TEST|pwds=`gpg --decrypt ~/test.gpg` \& eval "$pwds"|gI' {} \;

例子:

$ cat file.txt
line 1
line 2 PHRASE=TEST
line 3

$ find ./ -type f -exec gsed -i 's/PHRASE=TEST/pwds=`gpg --decrypt ~\/test.gpg` \& eval "$pwds"/gI' {} \;

$ cat file.txt
line 1
line 2 pwds=`gpg --decrypt ~/test.gpg` & eval "$pwds"
line 3

【讨论】:

  • sed: -e expression #1, char 110: unterminated `s' command 虽然我使用了你的第二行,但我会尝试第一行
猜你喜欢
  • 2012-12-18
  • 1970-01-01
  • 2016-08-17
  • 2014-12-28
  • 2022-01-08
  • 1970-01-01
  • 2018-09-26
  • 2014-02-07
  • 2021-02-08
相关资源
最近更新 更多