Comparing two files?

Microsoft Windows
Post Reply
User avatar
MigrationUser
Posts: 336
Joined: 2021-Jul-12, 1:37 pm
Contact:

Comparing two files?

Post by MigrationUser »

20 Aug 2008 20:08
tatouage

I need to compare two files and display (or redirect) the result.
For example:

file1:

andy
peter
mark
paul

file2:

andy
peter
paul

The result should be:

mark

Nothing more, nothing less.

The "fc" gives me too much information.

anyone an idea?

thx in advance

----------------------------

#2 28 Aug 2008 18:53
Simon Sheppard


Something like this - untested

Code: Select all

SETLOCAL EnableDelayedExpansion
FOR /F "tokens=1" %%G IN (file1.txt) DO (
  set _answer=%%G
  FOR /F "tokens=1" %%K IN (file2.txt)  DO (if "%%G"=="%%K" set _answer="")
  echo !_answer!
)
----------------------------

#3 28 Aug 2008 22:05
bluesxman


Don't think that's gonna cut it, Simon -- I reckon that'll be showing you this:

.
andy
andy
peter
.
peter
mark
mark
mark
paul
paul
.

Where "." is representing a blank line being displayed due to finding a match.

I guess the lack of a match could be caught and used to flag a difference, but the effectiveness will rather depend on the data being compared and what exactly you want to get out of it.

Also, if the files are too long, it'd take an age to run -- two files with just 10 lines each will cause 100 comparisons.

I doubt there's a straight forward way to implement a decent file comparison in .cmd

Better off finding some third party tool out there that will do the necessary. Or find one that at least has output that you can manipulate more easily to suit your needs than that produced by "FC". The "diff" program included in "cygwin" might be an option.

Last edited by bluesxman (28 Aug 2008 22:07)

cmd | *sh | ruby | chef

----------------------------

#4 29 Aug 2008 18:12
Simon Sheppard


Not quite, tatouage is looking for the lines that *dont* match

heres what the first script returns:

""
""
mark
""

To get rid of the extra lines try this:

Code: Select all

@echo off
SETLOCAL EnableDelayedExpansion
FOR /F "tokens=1" %%G IN (file1.txt) DO (
   set _answer=%%G
   FOR /F "tokens=1" %%K IN (file2.txt)  DO (if "%%G"=="%%K" set _answer=)
   if defined _answer echo !_answer!
)
I agree the performance might not be very good if there are lots of lines in either file.

Just for comparison, heres how you would do it in Powershell - just one line, a lot easier I think :)

Code: Select all

cat file1.txt | where { (cat file2.txt) -notcontains $_ }
----------------------------

#5 30 Aug 2008 10:21
bluesxman


Sorry -- misread your bracketing! I'll get my coat.

cmd | *sh | ruby | chef

----------------------------

#6 20 Feb 2009 17:47
killavirus


This is excellent :)

how would i go about to make it non case sensitive

If its a biggie its not that important

----------------------------

#7 22 Feb 2009 13:56
bluesxman


Using "if /i" does a case insensitive comparison ... unless you're talking about the PowerShell solution, in which case I don't know.

Last edited by bluesxman (22 Feb 2009 13:58)

cmd | *sh | ruby | chef

----------------------------

#8 26 Feb 2009 17:46
killavirus


no as always your bang on target !!!

thank you v much
Post Reply