1.

Solve : Listing lines which appear in FILE1.TXT but not FILE2.TXT?

Answer»

Hello,

Is there any way I can get a listing of text lines which do appear in FILE1.TXT but not in FILE2.TXT ?

For example, if FILE1.TXT contains the following text....

C:\January
C:\February
C:\March
C:\April
C:\May
C:\June

....and FILE2.TXT contains the following text....

C:\June
C:\May
C:\March
C:\February
C:\January

....then the output listing would be, for example....

C:\April


THANK YOU FOR YOUR TIME,
James

P.S. I am using Windows XP [Version 5.1.2600] cmd.exe

Thanks,
JamesNormally files to be compared should be in sequence (not calendar sequence but ascii or unicode sequence). Batch code has no random or direct i/o only sequential which can only be used in the CONFINES of a loop which the user cannot directly control.

With XP, I would suggest a Windows Script using the dictionary object.

Check out these Four Guys

Good luck.

Quote

Normally files to be compared should be in sequence (not calendar sequence but ascii or unicode sequence). Batch code has no random or direct i/o only sequential which can only be used in the confines of a loop which the user cannot directly control.

I am unsure of the RELEVANCE of these remarks?

Code: [Select] @echo off

REM Quick hack...
REM diffline.cmd
REM syntax diffline file1 file2

REM in a loop, read every line in file 1

for /F "delims==" %%a in (%1) do (

REM and try to find it in file 2
REM echo the line if NOT found

type %2 | find "%%a"> nul || echo %%a

)

REM in a loop, read every line in file 2

for /F "delims==" %%a in (%2) do (

REM and try to find it in file 1
REM echo the line if NOT found

type %1 | find "%%a"> nul || echo %%a

)




Code: [Select]
I:\test>type file1.txt
C:\January
C:\February
C:\March
C:\April
C:\May
C:\June

I:\test>type file2.txt
C:\June
C:\May
C:\March
C:\February
C:\January

I:\test>diffline file1.txt file2.txt
C:\April

I:\test>


Code: [Select]I:\test>diffline file1.txt file2.txt > diff.txt

I:\test>type diff.txt
C:\April

I:\test>

Thanks contrex. That's brilliant.

One further slight complication / question... :-)

Is there any way to get it working if FILE1.TXT contains a double-quote character? Find.exe reports an error SAYING "Parameter format not correct" if its search string contains double-quote.

Not to worry if this isn't possible, because I have a utility (called FRT.EXE -- Find and Replace Text) which can pre-processs the files by SUBSTITUTING all double-quotes (hex 22) with two single-quotes:-
Code: [Select] @echo off
REM Quick hack
FRT.EXE -C %1 \x22 ''
FRT.EXE -C %2 \x22 ''
for /F "delims=" %%L in (%1) do (type %2|FIND.EXE "%%L">nul || echo %%L)
Thanks again,
JamesHi contrex,

I did find a small bug in the above code, regarding listing text lines which appear in FILE1.TXT but not FILE2.TXT.

The good news is I've fixed the bug. See new version below, which also handles double-quotes without needing any pre-processing :-

Code: [Select]C:\> type diffline2.bat
@for /F "delims=" %%L in (%1) do @( set DIFF_LINE1=%%L& set DIFF_LINE1=!DIFF_LINE1:"=DOUBLEQUOTE!
set DIFF_FOUND=0
for /F "delims=" %%M in (%2) do @( set DIFF_LINE2=%%M& set DIFF_LINE2=!DIFF_LINE2:"=DOUBLEQUOTE!
if "!DIFF_LINE1!"=="!DIFF_LINE2!" set DIFF_FOUND=1
)
if !DIFF_FOUND!==0 echo %%L
)


C:\> type file1.txt
C:\DIR1
C:\DIR12
C:\DIR123

C:\> type file2.txt
C:\DIR1
C:\DIR123

C:\> diffline2 file1.txt file2.txt
C:\DIR12

C:\>

NOTE that this batch file MUST be run with cmd.exe "/V" flag, because it requires "delayed environment variable expansion". Batch file will NOT work correctly without cmd.exe "/V" flag.

Batch file tested using Windows XP [Version 5.1.2600].

Best regards,
JamesThis batch file can also be used to list lines which are output from 'COMMAND1' but not output from 'COMMAND2'. For example:-

Code: [Select]C:\> DIR /b folder1
alpha.txt
beta.txt
gamma.txt

C:\> DIR /b folder2
alpha.txt
gamma.txt

C:\> diffline2 '"DIR /b folder1"' '"DIR /b folder2"'
beta.txt

C:\>

(Note batch file must be run with cmd.exe "/V" flag, because it requires "delayed environment variable expansion"..)

Regards,
JamesFound another couple of bugs in the above DIFFLINE2.BAT :-

(1) Failed to work correctly for text line STARTING with ";" (simi-colon)
(2) Failed to work correctly for text line containing "!" (explanation mark)

I've fixed the first of these bugs. See DIFFLINE3.BAT below. Cannot fix second bug. Work-around is to pre-processs files by substituting all explanation marks with another character.

Code: [Select]D:\> type DIFFLINE3.BAT
@echo off
REM **** DIFFLINE VERSION 3 (1st May 2007)
REM **** List text lines which appear in "%1" but not in "%2"
REM **** MUST be run using "CMD /V" to enable delayed environment variable expansion
REM **** FAILS to work correctly if text lines contain "!" (explanation mark characters)

for /F "eol= delims=" %%L in (%1) do ( set DIFF_LINE1=%%L& set DIFF_LINE1=!DIFF_LINE1:"=DOUBLEQUOTE!
set DIFF_FOUND=0
for /F "eol= delims=" %%M in (%2) do ( set DIFF_LINE2=%%M& set DIFF_LINE2=!DIFF_LINE2:"=DOUBLEQUOTE!
if "!DIFF_LINE1!"=="!DIFF_LINE2!" set DIFF_FOUND=1
)
if !DIFF_FOUND!==0 echo %%L
)


D:\> type test1.txt
;
;;;
!
!!
!!!
"
"""
&
&&
&&&
<
<<<
|
||
|||
>
>>>
;!"&<|>
;!"&<|>.
;!"&<|>..

D:\> type test2.txt
;
;;
;;;
!
!!!
"
""
"""
&
&&&
<
<<
<<<
|
|||
>
>>
>>>
;!"&<|>
;!"&<|>..

D:\> DIFFLINE3 test1.txt test2.txt
&&
||
;"&<|>.

D:\> DIFFLINE3 test2.txt test1.txt
;;
""
<<
>>

D:\>

Regards,
James


Discussion

No Comment Found