compare text files and ..

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
code
Posts: 6
Joined: 30 May 2015 15:52

compare text files and ..

#1 Post by code » 11 Jun 2015 23:57

Good morning, I would need your help , I have to compare two files .txt (file1.txt file2.txt) both contain a list of ip but file 2.txt are the list ip updated and file1.txt is old list ip ...I want get a result in another file txt as a result ip missing in old file 1.txt , comand fc not work wel , because it gives me the difference understood as tabulation , because give me the difference in paragraph , I want to get from comparison from old file1.txt updated file2.txt List of IP that are not present in the old file 1.txt
thanks a lot

foxidrive
Expert
Posts: 6033
Joined: 10 Feb 2012 02:20

Re: compare text files and ..

#2 Post by foxidrive » 12 Jun 2015 03:08

Test this:

Code: Select all

type file2.txt|findstr /v /l /g:file1.txt

code
Posts: 6
Joined: 30 May 2015 15:52

Re: compare text files and ..

#3 Post by code » 12 Jun 2015 12:23

I thank you very much for the help you give , seems work, I only added that in order to have the result in a file txt

type file2.txt|findstr /v /l /g:file1.txt >namefile.txt and the path of the file

thanks again for all

Ben Mar
Posts: 22
Joined: 03 May 2015 10:51

Re: compare text files and ..

#4 Post by Ben Mar » 12 Jun 2015 18:42

foxidrive wrote:Test this:

Code: Select all

type file2.txt|findstr /v /l /g:file1.txt

Let's assume the content of file1.txt is like this:

Code: Select all

127.0.0.1
127.0.0.3
127.0.0.5
127.0.0.7
127.0.0.9
127.0.0.11
127.0.0.13
127.0.0.15


Let's assume the content of file2.txt is like this:

Code: Select all

127.0.0.1
127.0.0.2
127.0.0.3
127.0.0.4
127.0.0.5
127.0.0.6
127.0.0.7
127.0.0.8
127.0.0.9
127.0.0.10
127.0.0.11
127.0.0.12
127.0.0.13
127.0.0.14
127.0.0.15
127.0.0.16


The above code just give this result:

Code: Select all

127.0.0.2
127.0.0.4
127.0.0.6
127.0.0.8


I come up with the normal comparison method like this:

Code: Select all

@echo off
setlocal enabledelayedexpansion
(
   for /f %%A in (file2.txt) do (
      set found=0
      for /f %%B in (file1.txt) do (
          if "%%A"=="%%B" set found=1
      )
      if !found! equ 0 echo %%A
   )
)>namefile.txt


and it will give the expected result:

Code: Select all

127.0.0.2
127.0.0.4
127.0.0.6
127.0.0.8
127.0.0.10
127.0.0.12
127.0.0.14
127.0.0.16

Compo
Posts: 487
Joined: 21 Mar 2014 08:50

Re: compare text files and ..

#5 Post by Compo » 13 Jun 2015 00:28

Here:

Code: Select all

FindStr/vxg:file1.txt file2.txt>namefile.txt

foxidrive
Expert
Posts: 6033
Joined: 10 Feb 2012 02:20

Re: compare text files and ..

#6 Post by foxidrive » 13 Jun 2015 03:33

This should also solve it:

Code: Select all

type file2.txt|findstr /v /l /b /e /g:file1.txt

Compo
Posts: 487
Joined: 21 Mar 2014 08:50

Re: compare text files and ..

#7 Post by Compo » 13 Jun 2015 06:55

@foxidrive, regardless of any benefits of:
  • /x string
  • ^string$
  • /b /e string
  • \<string\>
What is, if any, the benefit of using Type and piping it into FindStr over the documented

Code: Select all

FindStr [/Option(s)] String(s) FileName

foxidrive
Expert
Posts: 6033
Joined: 10 Feb 2012 02:20

Re: compare text files and ..

#8 Post by foxidrive » 13 Jun 2015 08:33

Compo wrote:What is, if any, the benefit of using Type and piping it into FindStr


None! Did I say that there was? ;)

It's more inefficient as you probably well know - I didn't bother changing it to your better style because I'm still a bit crook and I just copy and pasted.

I have trouble concentrating and just flick through all the recent discoveries and code, too much for a bear with little brain.

code
Posts: 6
Joined: 30 May 2015 15:52

Re: compare text files and ..

#9 Post by code » 15 Jun 2015 15:22

thanks to everyone , I try , good job Compo , Ben Mar , Foxi , very helpful for me

Post Reply