【发布时间】:2016-11-26 21:17:45
【问题描述】:
以下works in Javascript 匹配:
[^\$]fileref.*
但是,在 sed 中,正则表达式不匹配任何内容。我想在忽略变量标识符的同时替换 bash 文件中的变量引用。前提是我有一个默认占位符文件,需要使用 sed -i 从 shell 脚本进行更新。我找不到 sed 对此表达式有问题的原因。
Sed 测试示例:
echo -e 'fileref=old\n./executable $fileref' | sed 's/[^\$]fileref.*/fileref=replaced/g'
gnu sed(ubuntu 或 centOS)的输出,其中未找到匹配项:
fileref=old
./executable $fileref
期望的输出:
fileref=replaced
./executable $fileref
【问题讨论】:
-
sed 逐行工作,"fileref" 前没有字符。如果要匹配行首,请使用
^锚点。顺便说一句,您不需要在字符类中转义$。