13

This may be a stupid question but even after reading the documentation I'm still not sure how to find all lines in a file that contain either ".cpp" or ".h".

Is there a way to do that?

For example (doesn't work):

findstr /i .cpp,.h // , == OR

3 Answers 3

12

You can do a logical OR by separating expressions with a space.

Give this a shot, should work for you.

findstr /i "\.cpp \.h" myfile.txt

Wrap both together within quotes and separate with a space. Note, you also have to escape the . in both of them.

1
  • Note: if you want to include a space in the expression, you can do it by putting it in a character class, e.g. findstr "hello[ ]world". (In my tests, backslash-escaping on the space seems to do a logical AND.)
    – mwfearnley
    Dec 23, 2022 at 8:56
6

If you need to include phrases (Space separated words) you can do it:

findstr /i /c:"IBSS" /c:"WDI Version" /c:"Soft Ap"

Or if you are getting some unwanted rows, you can exclude them using the /v parameter too:

findstr /i /c:Hosted /c:Version /c:Driver | findstr /v /i /c:drivers /c:native

(Tested this on Windows 7 and Windows 10)

2
  • 1
    Note, For efficiently processing Large Datasets, it is prefferable to avoid the second FindStr command when Possible, or to reduce the lines being fed to it when not. This is possible where terms to be exluded overlap in some way with the terms to be included by using the simplified Regex Comparisons offered by the FindStr command. In the example given matching "Driver" but excluding "DriverS" has the potential to be faster on large datasets by reducing the lines passed to the second FindStr to evaluate. ie FindStr /I /R /C:"Hosted" /C:"Version /C:"Driver[^S]" | FindStr /v /i /c:"Native" Feb 15, 2019 at 15:13
  • Also, if you know your particular dataset well enough you may already know that most of the terms you wish to exclude account for the majority of your dataset, then it can be beneficial to place the exclusion statements first ie FindStr /V /I /C:"Native" /C:"Drivers" | FindStr /I /R /C:"Hosted" /C:"Version /C:"Driver" Feb 15, 2019 at 15:19
0

How you form the OR logic depends on what you're looking for and your anticipated future changes, if applicable.

If it's a list of queries that have no whitespace in them, you can just do it like this, quick & dirty:

findstr "thing1 thing2 thing3 thing4 thing5" and if any of those things are present, it will evaluate to true and exit 0.

If what you're looking for does have whitespace, you have to use findstr's literal search strings with the /c:"" type of parameter. You can still use this type of parameter if there is no whitespace.

/C:string Uses specified string as a literal search string.

However, you cannot combine literal with quick & dirty. For example, this won't work:

findstr "thing1 thing2" /c:"thing3 thingy"

You'd need to convert the earlier parameters to literal parameters, like so:

findstr /c:"thing1" /c:"thing2" /c:"thing3 thingy"

Because of this, it's a better practice to use the literal option because it will scale better with dynamic scripts. However if it's just a static set of queries that have no whitespace, the quick & dirty way should suffice.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .