MilqueToasted
MilqueToasted

Reputation: 83

batch file find to parse out keywords

I have a list of words stored in a text file called blacklist.txt I want to go through output from another program and take out all the lines that contain any of these words.

if i do this:

for /f %%G in (blacklist.txt) find /v /i "%%G" output.txt > newoutput.txt

I only get the results form the last find

if i do this:

for /f %%G in (blacklist.txt) find /v /i "%%G" output.txt > output.txt

I would expect it to update the file and run the next find on it systematically filtering out all the blacklisted strings. This however is not the case and the file becomes blank after the second find is run on it...

Has anyone tried doing something similar to this before?

Upvotes: 2

Views: 2334

Answers (5)

bobbogo
bobbogo

Reputation: 15523

Hmmm. I note findstr has both /v and /g:file. This means that you can forget about the for loop.

findstr /v /l /g:blacklist.txt output.txt >tmp
move tmp output.txt

Upvotes: 1

bobbogo
bobbogo

Reputation: 15523

for /f %%g in (blacklist.txt) do (
    find /v /i "%1" <output.txt >tmp
    move tmp output.txt
)

Note that getting find to read from stdin means you won't get spurious ---------- output.txt lines appearing in the output.

Upvotes: 1

BertV
BertV

Reputation: 81

Here is what I mean. Put following in a batch file and run it:

for /f %%G in (blacklist.txt) do call :finder %%G
goto :EOF

:finder
find /v /i "%1" output.txt > output.tmp
copy output.tmp output.txt

The output.txt will contain non-matching lines. It will also contain multiple times the name of the input file. To avoid this, you can use the findstr instead of find command.

Upvotes: 2

BertV
BertV

Reputation: 81

use >> instead of >
Just don't forget to remove the output file for a second run, >> will always append to an existing file And you cannot redirect to the same file as the input, this is not support in a cmd.exe under windows

Upvotes: 0

aphoria
aphoria

Reputation: 20209

If you want to append to the file, change > to >>. Also, remove the space before the file name.

for /f %%G in (blacklist.txt) find /v /i "%%G" output.txt >>newoutput.txt

Upvotes: 1

Related Questions