在脚本中使用字符 »<« 和 »>« 时出现“sed”错误

“sed” error when using characters »<« and »>« in script

我正在尝试从某些源代码中删除一些 cpp #include 语句。我决定使用名为 "sed" 的非常强大的实用程序来执行此操作。当我尝试以下列方式执行 "sed.exe" 时:

sed -re '/#include \<syslog\.h\>/ d' < syslog.h > changed_syslog.h
sed -re '/#include <syslog\.h>/ d' < syslog.h > changed_syslog.h

我收到错误:

sed: -e expression #1, char 13: unterminated address rege

如果我尝试以下行:

sed -re '/#include .syslog\.h./ d' < syslog.h > changed_syslog.h

然后一切都按预期工作。

现在我想知道我在前两个命令中做错了什么?

我正在使用来自 Cygwin 1.7.17 的 sed.exe。

syslog.h 文件如下所示(只有一行):

#include <syslog.h>

sed 抱怨 'address regular expression' 未终止。地址正则表达式是 /#include \<syslog\.h\>/,因此这意味着 sed 没有看到最终的 /.

在这种情况下,问题在于 Windows shell 将 <syslog\.h> 中的 <> 字符视为重定向。解决方案是使用 ^:

引用它们
sed -re '/#include ^<syslog\.h^>/ d'