windows - How can I make a batch file that will tell me which lines of a text file are NOT in another file? -
what i'm trying take text file bunch of strings search for, each on own line, , search each 1 of these strings in file (check.txt). want output text file list of strings couldn't found. i've tried few things far.
/f "tokens=*" %%a in search.txt ( @echo on findstr %%a check.txt if errorlevel 1 echo %%a fail > fail_match.txt )
another attempt made (this 1 tell me if whole list or not)
@echo on findstr /g:search.txt check.txt > a_match.txt if errorlevel 1 echo bad > a_match.txt
i realize these incredibly basic, , i'm sure there's easy answer don't understand. i'm not programmer; want make job lot easier (and faster).
to clarify, list of things search in search.txt, list of things check them against check.txt. check.txt json file, it's 1 enormous line. don't know if make difference or not. want list of lines in search.txt not in check.txt.
your search scheme seems naive on 2 fronts:
1) json not guaranteed single line. valid jason may have amount of whitespace, including newlines. cause problems if search string logically matches across multiple lines.
2) substring matches? suppose 1 search string bat
, , json contains bath
. doubt want consider match.
it possible neither of above concerns problem case. assuming aren't, there may simple solution using findstr.
you close on first try, except
a) - /f in()
clause missing parentheses
b) - want force each search string interpreted string literal, possibly spaces. requires /c:
option.
c) - assume leading spaces not significant in search string ("tokens=*"
strips leading spaces)
d) - assume no search lines begin semicolon. (the default eof character semicolon, , /f skips lines begin eof character)
e) - quotes , backslashes must escaped within search string:
\" -> \\\\\"
, \ -> \\
, " -> \"
. see what undocumented features , limitations of windows findstr command? more information.
points c) , d) may fixed disabling eof , delims using following odd syntax:
for delims^=^ eof^= %%a in ...
point e) can addressed defining variable , adding escape sequences via search , replace. requires delayed expansion, delayed expansion corrupt /f variables upon expansion if contain !
. delayed expansion must strategically toggled on , off within loop.
instead of using if errorleven n
, can use conditional command concatenation ||
take action if previous command failed.
you don't need see output of findstr command, can redirected nul.
you can improve performance redirecting once, outside loop.
@echo off setlocal disabledelayedexpansion >fail_match.txt ( /f delims^=^ eol^= %%a in (search.txt) ( set "search=%%a" setlocal enabledelayedexpansion set "search2=!search:\"=\\"!" set "search2=!search2:\=\\!" set "search2=!search2:"=\"!" findstr /c:"!search2!" check.txt >nul || echo !search! endlocal ) )
if none of search strings begin ;
, , no search string contains "
or \
, solution can simple as:
@echo off setlocal disabledelayedexpansion >fail_match.txt ( /f "delims=" %%a in (search.txt) findstr /c:"%%a" check.txt >nul || echo %%a )
Comments
Post a Comment