【发布时间】:2011-03-08 13:45:42
【问题描述】:
我正在尝试使用 linux sed 命令将路径元素附加到 .htaccess 文件中的 RewriteBase。
我已经用这个参数试过了:
#current RewriteBase is "RewriteBase /mypath"
sed -ie 's/RewriteBase\(.*\)/RewriteBase \1\/add/g' .htaccess
结果如下:
addriteBase /mypath
所以 sed 用最后一个字符串覆盖替换字符串的开头。
这是为什么呢?
另一个问题是,当 RewriteBase 只是“/”时,如何防止出现 2 个斜线。
RewriteBase /
会变成
RewriteBase //add
但应该是
RewriteBase /add
有什么办法可以防止这种情况发生吗?如果不是,我可以运行第二个 sed 命令,将所有双斜杠替换为一个。但也许有更优雅的方式来做到这一点。
我将不胜感激。
【问题讨论】:
标签: regex linux text replace sed