FINDSTR - Can you do a multiple regex search?

T

Tom Del Rosso

This is a question that might turn into a tutorial. After composing a
question I thought I should experiment some more, and I found the answer,
which is at the end.

===begin what was supposed to be a question===

With FINDSTR you can search for multiple strings with an OR function, such
as

FINDSTR "Windows Linux"

which outputs lines containing either word.

That is useful when you want the filtered output lines to be in the
original order. If you did this instead

FIND "Windows"
FIND "Linux"

then the output would have all the Windows lines together and all the Linux
lines together. I want the original order, so I need the OR function that
FINDSTR does.

But I also want some of my search terms to contain spaces, like if a file
contained this

Windows
Linux
Gates
Gates is a schmuck

In the above file I don't want to find the third line.

So I tried embedded quotes, and I tried embedded single quotes, and I tried
the regex wildcard character (the period) like this

FINDSTR "Windows Linux Gates.is.a.schmuck"

in which the period should match spaces, but it doesn't work.

Does anybody know a way to do this, or do I have to use AWK?

===end what was supposed to be a question===


Ok, the answer I just found is that you must put all of the regular
expressions first, then all of the single words, like this

FINDSTR "Gates.is.a.schmuck Windows Linux"

or, with more than one multi-word phrase

FINDSTR "Gates.is.a.schmuck Jobs.has.some.ego Windows Linux"

I tested the last syntax on a text file containing this:

1 Windows
2 Gates is a schmuck Jobs has some ego
3 Gates is a schmuck some ego Jobs has
4 a schmuck Gates is Jobs has some ego
5 a schmuck Gates is some ego Jobs has
6 Gates a schmuck Jobs some ego
7 Gates is a real schmuck Jobs has got some ego
8 Linux

It returned lines 1, 2, 3, 4, 8 just like it should.

And BTW, FINDSTR can't read unicode, but TYPE can, and it outputs ANSI, so
this works on unicode files:

TYPE file.txt|FINDSTR "Gates.is.a.schmuck Jobs.has.some.ego Windows Linux"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top