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
Server Fault is a question and answer site for system and network administrators. It only takes a minute to sign up.
Sign up to join this communityThis 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
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.
findstr "hello[ ]world"
. (In my tests, backslash-escaping on the space seems to do a logical AND.)
Dec 23, 2022 at 8:56
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)
FindStr /I /R /C:"Hosted" /C:"Version /C:"Driver[^S]" | FindStr /v /i /c:"Native"
Feb 15, 2019 at 15:13
FindStr /V /I /C:"Native" /C:"Drivers" | FindStr /I /R /C:"Hosted" /C:"Version /C:"Driver"
Feb 15, 2019 at 15:19
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.