findstr DOS Command's multiple string argument
How do I filter words "black" and "white"?
The following command will display all lines containing "black"
NOR "white"
:
findstr /v "black white" blackwhite.txt
The following command will display all lines containing "black"
OR "white"
:
findstr "black white" blackwhite.txt
The following command will display all lines containing EXACTLY "black white
":
findstr /c:"black white" blackwhite.txt
The following command will display all lines containing "black"
AND "white"
:
findstr "white" blackwhite.txt | findstr "black"
Notes:
When the search string contains multiple words, separated with spaces, then
findstr
will return lines that contain either word (OR).A literal search (
/C:string
) will reverse this behaviour and allow searching for a phrase or sentence. A literal search also allow searching for punctuation characters.
Example data file (blackwhite.txt):
red
black
white
blue
black white
black and white
Example output:
F:\test>findstr /v "black white" blackwhite.txt
red
blue
F:\test>findstr "black white" blackwhite.txt
black
white
black white
black and white
F:\test>findstr /c:"black white" blackwhite.txt
black white
F:\test>findstr "white" blackwhite.txt | findstr "black"
black white
black and white
Further Reading
- An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
- findstr - Search for strings in files.