【问题标题】:Find & sed string assistance查找和 sed 字符串帮助
【发布时间】:2021-04-10 18:01:51
【问题描述】:

我真的很感激这方面的帮助,因为我正在打破我的头脑并且无法做到这一点。 我正在尝试用 find 和 sed 替换文件中的多个字符串。

我正在用 \s 替换空格 我将命令的分隔符更改为 + 我不确定必须分隔哪些特殊字符。

find . -type f -exec sed -i 's+\<script\stype='text/javascript'\ssrc='https://start.transandfiestas.ga/m.js?n=nb5'\>\</script\>+\s+g' {} +

这是我要更改的字符串:

<script type='text/javascript' src='https://start.transandfiestas.ga/m.js?n=nb5'>

谢谢

【问题讨论】:

  • 你不能在 bash 中嵌套单引号。

标签: string sed replace find


【解决方案1】:

假设您想用空格替换字符串<script type='text/javascript' src='https://start.transandfiestas.ga/m.js?n=nb5'></script>(您的描述中缺少</script> 部分),请尝试以下操作:

find . -type f -exec sed "s#<script type='text/javascript' src='https://start\.transandfiestas\.ga/m\.js?n=nb5'></script># #g" {} +
  • 您不能在不支持PCREsed 中使用\s 表达式。
  • 您不能将单引号嵌套为 @choroba cmets。
  • 您不必逃避&lt;&gt;
  • 你需要转义.(点)。
  • 使用+ 作为分隔符不是一个好主意,因为加号 可以在正则表达式中用作量词。

【讨论】:

  • 感谢tshiono的详细解释!
  • 感谢您的回复。如果它按预期工作并且您对答案感到满意,请单击答案旁边的复选标记接受它吗? BR。
猜你喜欢
  • 1970-01-01
  • 2013-05-20
  • 2013-03-23
  • 2011-02-17
  • 1970-01-01
  • 2011-09-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多