36
findstr /v "black"  File1.txt

Above DOS command will display content of 'File1.txt' which are not matching string "black".

How to modify this command , if I need to filter words "black" and "white" ?

Michel de Ruiter
  • 1,118
  • 13
  • 23
Arun
  • 527
  • 1
  • 7
  • 15
  • 2
    The `findstr` tool is not part of MS-DOS. It comes with Windows (XP+?). I think you mean 'command line tool' instead of 'DOS command'. – Michel de Ruiter Jul 15 '16 at 09:17

4 Answers4

55

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

DavidPostill
  • 153,128
  • 77
  • 353
  • 394
  • 1
    very interesting.. I guess this would be searching for white AND black `findstr "white" File2.txt | findstr "black"` – barlop Jul 17 '15 at 02:33
  • well, since we have NOR, So there is still a permutation we could consider missing. NAND. Another one we could consider missing, is XOR – barlop Jul 17 '15 at 10:55
  • @barlop I can't figure out how to do NAND or XOR :/ I know what the output **should** be but how to get there ... – DavidPostill Jul 17 '15 at 12:06
  • maybe there isn't a nice quick way, it'd probably be a batch file checking errorlevel probably better to use some other tool if doing that, looks like grep can't,. But awk can do quite a bit or of course perl http://unix.stackexchange.com/questions/177513/grep-with-logic-operators – barlop Jul 17 '15 at 12:29
  • Let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/25960/discussion-between-barlop-and-davidpostill). – barlop Jul 17 '15 at 12:31
1

If you need to display all lines with the words "black" or "white" then get rid of the /v in your command.

Try: findstr white File1.txt or findstr black File1.txt or findstr "black white" File1.txt

The /V operand will print all lines that DO NOT contain your search string.

Type findstr /? for more info on how to use findstr.

Dean Spicer
  • 641
  • 1
  • 5
  • 14
0

Wanted to add to this to mention how to use findstr with stdout on Windows for multiple searches.

Here is how to list only network interface names and their ip addresses:

ipconfig /all | findstr /i /L /c:"Ethernet" /c:"IPv4"

The Microsoft Documentation details each parameter

  • /i Ignores the case of the characters when searching for the string.
  • /l Processes search strings literally.
  • /c:<string> Uses the specified text as a literal search string.
Dave
  • 113
  • 1
  • 6
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 23 '22 at 19:35
0

Here is another example using a different output method, not just a straight file search. For example check for network port status where port = 98765 and status = ESTABLISHED or LISTENING.

For example this runs a netstat every five seconds, then pipes the netstat output to a FIND (could also use FINDSTR), then pipes those results to findstr to filter when the port is in a certain state (in this case ESTABLISHED or LISTENING).

netstat -an 5 | find "98765" | findstr "ESTABLISHED LISTENING"

TCP    192.168.123.456:57349     10.10.12.34:98765 ESTABLISHED
Aulis Ronkainen
  • 2,612
  • 42
  • 29
  • 27
MisterB
  • 1
  • 1
  • 1
    Welcome to Super User! Before answering an old question having an accepted answer (look for green ✓) as well as other answers ensure your answer adds something new or is otherwise helpful in relation to them. Here is a guide on [answer]. There is also [tour] for the site tour, and [help] for the help center. – help-info.de Jun 02 '22 at 06:45