Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Listing lines which appear in FILE1.TXT but not FILE2.TXT  (Read 3403 times)

0 Members and 1 Guest are viewing this topic.

James2000

  • Guest
Listing lines which appear in FILE1.TXT but not FILE2.TXT
« on: April 29, 2007, 12:07:52 PM »
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


James2000

  • Guest
Re: Listing lines which appear in FILE1.TXT but not FILE2.TXT
« Reply #1 on: April 29, 2007, 12:11:41 PM »
P.S.  I am using Windows XP [Version 5.1.2600]  cmd.exe

Thanks,
James

Sidewinder



    Guru

    Thanked: 139
  • Experience: Familiar
  • OS: Windows 10
Re: Listing lines which appear in FILE1.TXT but not FILE2.TXT
« Reply #2 on: April 29, 2007, 02:20:45 PM »
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.

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

Check out these Four Guys

Good luck. 8)

« Last Edit: April 29, 2007, 02:40:56 PM by Sidewinder »
The true sign of intelligence is not knowledge but imagination.

-- Albert Einstein

contrex

  • Guest
Re: Listing lines which appear in FILE1.TXT but not FILE2.TXT
« Reply #3 on: April 29, 2007, 03:32:20 PM »
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>


James2000

  • Guest
Re: Listing lines which appear in FILE1.TXT but not FILE2.TXT
« Reply #4 on: April 30, 2007, 08:26:47 AM »
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,
James

James2000

  • Guest
Re: Listing lines which appear in FILE1.TXT but not FILE2.TXT
« Reply #5 on: April 30, 2007, 10:55:21 AM »
Hi 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,
James

James2000

  • Guest
Listing lines which output from 'COMMAND1' but not 'COMMAND2'
« Reply #6 on: April 30, 2007, 11:48:25 AM »
This 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,
James

James2000

  • Guest
Re: Listing lines which appear in FILE1.TXT but not FILE2.TXT
« Reply #7 on: May 01, 2007, 07:27:04 AM »
Found 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