开发者

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 t开发者_StackOverflow社区he 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?


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.


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.


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


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜