考虑文件test.txt
:
#include "foo.h"
#include "bar.h"
#include "baz.h"
使用 GNU sed 版本 4.2.1(在 Ubuntu 10.04.4 LTS 上),我可以通过以下方式提取 foo.h、bar.h 和 baz.h:
SHELL$) sed -n -e 's:^s*#includes*"(.*)".*:1:p' test.txt
foo.h
bar.h
baz.h
使用 BSD sed(在 Mac OS X lion 上),并修改上述命令,我可以提取 foo.h、bar.h 和 baz.h,但用双引号:
SHELL) sed -n -e 's:^s*#includes*(.*).*:1:p' test.txt
"foo.h"
"bar.h"
"bar.h"
如何使用 BSD sed 提取没有引号的名称?这些命令的输出为空:
SHELL) sed -n -e 's:^s*#includes*"(.*)".*:1:p' test.txt
SHELL) sed -n -e 's:^s*#includes*"(.*)".*:1:p' test.txt
BSD sed
(不出所料,真的)不支持s
Perlism - 它被解释为只是一个字面s
。试试这个;
sed -n -e 's!^[[:space:]]*#include[[:space:]]*"(.*)".*!1!p' test.txt
字符类[[:space:]]
应该适用于所有 POSIX 正则表达式实现。(其他sed
可能需要也可能不需要在对括号进行分组之前使用反斜杠。