【问题标题】:How can I avoid creation of the extra file with `-e` while using the sed tool in a shell script?在 shell 脚本中使用 sed 工具时,如何避免使用 `-e` 创建额外文件?
【发布时间】:2017-02-27 12:03:16
【问题描述】:

我在 shell 脚本中使用sed 命令在 osx(unix) 环境中编辑和替换 xml 文件的某些文件内容。有什么方法可以避免 sed 使用 -e 创建临时文件?我在脚本中执行以下操作以使用 sed 编辑文件。

sed -i -e 's/abc/xyz/g' /PathTo/MyEditableFile.xml

sed 以上命令效果很好但会创建一个额外文件/PathTo/MyEditableFile.xml-e

我怎样才能避免创建带有-e 的额外文件?

我尝试了一些选项,例如将临时文件夹路径设置为 sed,以便它在 /tmp/ 中创建临时文件。像这样:

sed -i -e 's/abc/xyz/g' /PathTo/MyEditableFile.xml >/tmp

但是好像没用

【问题讨论】:

标签: bash macos shell sed


【解决方案1】:

当您在原地编辑文件 (-i) 时,OS X sed 需要强制备份文件名。

您可以使用 -i "" 来解决这个问题:

sed -i "" -e 's/abc/xyz/g' /PathTo/MyEditableFile.xml

【讨论】:

  • 非常感谢。有用。不过,删除 -e 对我不起作用。可能是因为我实际上正在执行复杂的 sed 命令。像这样:sed -i "" -e 's/searchcriteriawith="[^"]*/searchcriteriawith="'$new_value_frmSome_var'/g' MyEditableFile.xml
  • 我不想在我的问题中包含所有这些复杂性。因此简化了示例中的 sed 命令
  • @NelsonP 没问题。我也删除了那部分。
猜你喜欢
  • 2021-09-04
  • 2021-05-08
  • 1970-01-01
  • 1970-01-01
  • 2012-05-31
  • 2017-10-24
  • 2015-01-12
  • 2023-03-16
  • 2016-07-19
相关资源
最近更新 更多