Rajat Saxena
Rajat Saxena

Reputation: 3925

Windows batch: unable to filter out the exactly matching line

I am using this command to filter out the line set timing on;

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

and the output,I'm getting is as follows:

whenever sqlerror exit 9;

grant select on exfdsfds;


exit;

The content of filea.sql is:

set timing on;
whenever sqlerror exit 9;

grant select on exfdsfds;

timing on; cool

exit;

Why is it removing timing on; cool?What modifications to command are needed to avoid such results?

Upvotes: 0

Views: 77

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200293

As documented in findstr /? you need to use /c if you want to match a whole string containing spaces:

Use spaces to separate multiple search strings unless the argument is prefixed with /C. For example, 'FINDSTR "hello there" x.y' searches for "hello" or "there" in file x.y. 'FINDSTR /C:"hello there" x.y' searches for "hello there" in file x.y.

If you want to use ^ and $ you also need /r, otherwise the string will be treated as a literal string instead of a regular expression.

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

Without /c the command removes all lines containing any of the given words from the output, which includes the line timing on; cool because there's a match for the word timing.

Upvotes: 3

Related Questions