【发布时间】:2019-06-30 09:14:46
【问题描述】:
我正在尝试替换 wordpress sql 文件中的以下字符串:
http:\\/\\/firstdomain.com\\/qwerty\\/wp-content\\/uploads\\/2018\\/07\\/section-shape.png
到
https:\\/\\/seconddomain.com\\/wp-content\\/uploads\\/2019\\/06\\/section-shape.png
我尝试了以下命令,显然没有用
sed -i "s#'http:\\/\\/firstdomain.com\\/qwerty\\/wp-content\\/uploads\\/2018\\/07\\/section-shape.png'#'https:\\/\\/seconddomain.com\\/wp-content\\/uploads\\/2019\\/06\\/section-shape.png'#g" database.sql
请有人帮助了解我错过的地方。非常感谢。
【问题讨论】:
-
sed -i 's|http:\\\\/\\\\/firstdomain.com\\\\/qwerty\\\\/wp-content\\\\/uploads\\\\/2018\\\\/07\\\\/section-shape.png|https:\\\\/\\\\/seconddomain.com\\\\/wp-content\\\\/uploads\\\\/2019\\\\/06\\\\/section-shape.png|' database.sql -
谢谢赛勒斯,不幸的是这个组合并没有改变任何东西。
-
显然涉及
\[\]的数量。做一个更小的测试用例,让它工作。我见过我需要 5 个“\”字符的情况,但它可以是 1 到 5 之间的任何字符(也许更多!),所以尝试一下。祝你好运。 -
和 ...
echo "http:\\/\\/firstdomain.com\\/qwerty\\/wp-content\\/uploads\\/2018\\/07\\/section-shape.png" | sed 's#http:\\/\\/firstdomain.com\\/qwe rty\\/wp-content\\/uploads\\/2018\\/07\\/section-shape.png#https:\\/\\/seconddomain.com\\/wp-content\\/uploads\\/2019\\/06\\/section-shape. png#'产生您需要的输出。一定还有别的,你是不是想在php中这样做?为什么不在您的 Q 中包含一个实际文件的小样本,以便我们进行适当的测试?祝你好运。 -
@shelter 在我的脑海中浮现,我认为如果您需要超过 2 个反斜杠(甚至可能是 1 个),那么您的引用错误。例如,
echo "http:\\/\\/firstdomain.com\\/qwerty\\/wp-content\\/uploads\\/2018\\/07\\/section-shape.png"应该是echo 'http:\\/\\/firstdomain.com\\/qwerty\\/wp-content\\/uploads\\/2018\\/07\\/section-shape.png'(即单引号而不是双引号),因此 shell 不会删除每对引号中的一个。