Windows批处理:无法过滤掉完全匹配的行

时间:2013-01-20 13:16:37

标签: windows batch-file cmd

我正在使用此命令过滤掉行set timing on;

type filea.sql | findstr /v "^set timing on;$"

和输出,我得到如下:

whenever sqlerror exit 9;

grant select on exfdsfds;


exit;

filea.sql的内容是:

set timing on;
whenever sqlerror exit 9;

grant select on exfdsfds;

timing on; cool

exit;

为什么删除timing on; cool?需要对命令进行哪些修改才能避免此类结果?

1 个答案:

答案 0 :(得分:3)

findstr /?中所述,如果要匹配包含空格的整个字符串,则需要使用/c

  

使用空格分隔多个搜索字符串,除非参数以/ C为前缀。例如,'FINDSTR'你好“x.y”在文件x.y中搜索“hello”或“there”。 'FINDSTR / C:“你好”x.y'在文件x.y中搜索“你好那里”。

如果您想使用^$,还需要/r,否则该字符串将被视为文字字符串而非正则表达式。

type filea.sql | findstr /v /r /c:"^set timing on;$"

如果没有/c,该命令将删除包含输出中任何给定单词的所有行,其中包含行timing on; cool,因为单词timing匹配。