|
server
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Text File Compare
Folks:
I have 2 separate text file each containing a bunch of IP addresses. The first text file (TextIP-1.txt) contains 100 IP addresses; list 1 below the other. The second text file (TextIP-2.txt) contains 56 IP addresses; listed 1 below the other. I am look for a script that would allow me to compare the content of these 2 text files & identify those overlapping IP addresses that exist in BOTH of these text files. Maybe the results can be printed to a third text file. Any help appreciated. Thanks in advance. Jo. JoJo wrote:
Show quote > Folks: Since you are looking for IPs (as text lines) that are either common to both > > > I have 2 separate text file each containing a bunch of IP addresses. > The first text file (TextIP-1.txt) contains 100 IP addresses; list 1 > below the other. > The second text file (TextIP-2.txt) contains 56 IP addresses; listed > 1 below the other. > > I am look for a script that would allow me to compare the content of > these 2 text files & identify those overlapping IP addresses that > exist in BOTH of these text files. > Maybe the results can be printed to a third text file. Any help > appreciated. or unique to either one or the other, I would use a pair of Scripting.Dictionary objects as hash tables... A simple demo... '================================================================== fileA = "c:\temp\TextIP-1.txt" Set listA = CreateObject("Scripting.Dictionary") listA.CompareMode = vbTextCompare fileB = "c:\temp\TextIP-2.txt" Set listB = CreateObject("Scripting.Dictionary") listB.CompareMode = vbTextCompare Set fso = CreateObject("Scripting.FileSystemObject") with fso.OpenTextFile(fileA) Do Until .AtEndOfStream line = Trim(.ReadLine) If Len(line) Then listA(line) = True wscript.echo line, "loaded in listA" End If Loop .Close End with wscript.echo String(40,"-") with fso.OpenTextFile(fileB) Do Until .AtEndOfStream line = Trim(.ReadLine) If Len(line) Then listB(line) = True wscript.echo line, "loaded in listB" End If Loop .Close End with 'compare keys in listA with those in listB wscript.echo String(40,"-") For each key In listA.keys If listB.Exists(key) Then wscript.echo key, "in listA is in both lists" Else wscript.echo key, "in listA is not in listB" End If Next 'compare keys in listB with those in listA wscript.echo String(40,"-") For each key In listB.keys If listA.Exists(key) Then wscript.echo key, "in listB is in both lists" Else wscript.echo key, "in listB is not in listA" End If Next '================================================================== ---------- cscript ---------- Microsoft (R) Windows Script Host Version 5.6 Copyright (C) Microsoft Corporation 1996-2001. All rights reserved. 1.1.1.1 loaded in listA 2.2.2.2 loaded in listA 4.4.4.4 loaded in listA 5.5.5.5 loaded in listA 6.6.6.6 loaded in listA 8.8.8.8 loaded in listA 9.9.9.9 loaded in listA ---------------------------------------- 1.1.1.1 loaded in listB 3.3.3.3 loaded in listB 5.5.5.5 loaded in listB 7.7.7.7 loaded in listB 9.9.9.9 loaded in listB ---------------------------------------- 1.1.1.1 in listA is in both lists 2.2.2.2 in listA is not in listB 4.4.4.4 in listA is not in listB 5.5.5.5 in listA is in both lists 6.6.6.6 in listA is not in listB 8.8.8.8 in listA is not in listB 9.9.9.9 in listA is in both lists ---------------------------------------- 1.1.1.1 in listB is in both lists 3.3.3.3 in listB is not in listA 5.5.5.5 in listB is in both lists 7.7.7.7 in listB is not in listA 9.9.9.9 in listB is in both lists Output completed (0 sec consumed) - Normal Termination -- Michael Harris Microsoft.MVP.Scripting Mike:
Appreciate the suggestion. I saved your file as "Compare.VBS". Then I did "wscript compare.vbs" The Windows Script Host comes up stating "1.1..1.2 loaded in List A" If I click on OK, it moves to the next IP address and repeats the message. PS If it makes for a simpler solution, these text files can readily be converted to spreadsheets and the data stuffed in 1 column. Thanks anyway. Jo. Show quote "Michael Harris (MVP)" <mikhar.at.mvps.dot.org> wrote in message news:ek3qhLdiHHA.4984@TK2MSFTNGP06.phx.gbl... > JoJo wrote: >> Folks: >> >> >> I have 2 separate text file each containing a bunch of IP addresses. >> The first text file (TextIP-1.txt) contains 100 IP addresses; list 1 >> below the other. >> The second text file (TextIP-2.txt) contains 56 IP addresses; listed >> 1 below the other. >> >> I am look for a script that would allow me to compare the content of >> these 2 text files & identify those overlapping IP addresses that >> exist in BOTH of these text files. >> Maybe the results can be printed to a third text file. Any help >> appreciated. > > Since you are looking for IPs (as text lines) that are either common to > both or unique to either one or the other, I would use a pair of > Scripting.Dictionary objects as hash tables... > > A simple demo... > > > '================================================================== > fileA = "c:\temp\TextIP-1.txt" > Set listA = CreateObject("Scripting.Dictionary") > listA.CompareMode = vbTextCompare > > fileB = "c:\temp\TextIP-2.txt" > Set listB = CreateObject("Scripting.Dictionary") > listB.CompareMode = vbTextCompare > > Set fso = CreateObject("Scripting.FileSystemObject") > > with fso.OpenTextFile(fileA) > Do Until .AtEndOfStream > line = Trim(.ReadLine) > If Len(line) Then > listA(line) = True > wscript.echo line, "loaded in listA" > End If > Loop > .Close > End with > wscript.echo String(40,"-") > with fso.OpenTextFile(fileB) > Do Until .AtEndOfStream > line = Trim(.ReadLine) > If Len(line) Then > listB(line) = True > wscript.echo line, "loaded in listB" > End If > Loop > .Close > End with > > 'compare keys in listA with those in listB > wscript.echo String(40,"-") > For each key In listA.keys > If listB.Exists(key) Then > wscript.echo key, "in listA is in both lists" > Else > wscript.echo key, "in listA is not in listB" > End If > Next > > 'compare keys in listB with those in listA > wscript.echo String(40,"-") > For each key In listB.keys > If listA.Exists(key) Then > wscript.echo key, "in listB is in both lists" > Else > wscript.echo key, "in listB is not in listA" > End If > Next > '================================================================== > > ---------- cscript ---------- > Microsoft (R) Windows Script Host Version 5.6 > Copyright (C) Microsoft Corporation 1996-2001. All rights reserved. > > 1.1.1.1 loaded in listA > 2.2.2.2 loaded in listA > 4.4.4.4 loaded in listA > 5.5.5.5 loaded in listA > 6.6.6.6 loaded in listA > 8.8.8.8 loaded in listA > 9.9.9.9 loaded in listA > ---------------------------------------- > 1.1.1.1 loaded in listB > 3.3.3.3 loaded in listB > 5.5.5.5 loaded in listB > 7.7.7.7 loaded in listB > 9.9.9.9 loaded in listB > ---------------------------------------- > 1.1.1.1 in listA is in both lists > 2.2.2.2 in listA is not in listB > 4.4.4.4 in listA is not in listB > 5.5.5.5 in listA is in both lists > 6.6.6.6 in listA is not in listB > 8.8.8.8 in listA is not in listB > 9.9.9.9 in listA is in both lists > ---------------------------------------- > 1.1.1.1 in listB is in both lists > 3.3.3.3 in listB is not in listA > 5.5.5.5 in listB is in both lists > 7.7.7.7 in listB is not in listA > 9.9.9.9 in listB is in both lists > > Output completed (0 sec consumed) - Normal Termination > > -- > Michael Harris > Microsoft.MVP.Scripting > > On Sat, 28 Apr 2007 17:02:01 -0400, <JoJo> wrote in
microsoft.public.windows.server.scripting, microsoft.public.windows.powershell, microsoft.public.win2000.cmdprompt.admin: >Appreciate the suggestion. I saved your file as "Compare.VBS". Then I did See below.>"wscript compare.vbs" >The Windows Script Host comes up stating "1.1..1.2 loaded in List A" Have you tried "CSCRIPT compare.vbs"?>If I click on OK, it moves to the next IP address and repeats the message. [snip] >"Michael Harris (MVP)" wrote in message >news:ek3qhLdiHHA.4984@TK2MSFTNGP06.phx.gbl... [snip] >> A simple demo... [snip] >> ---------- cscript ---------- [snip]
Show quote
<JoJo> wrote in message news:e1522sciHHA.208@TK2MSFTNGP05.phx.gbl... Unfortunately, you crossposted this to groups of dissimilar languages.> Folks: > > > I have 2 separate text file each containing a bunch of IP addresses. > The first text file (TextIP-1.txt) contains 100 IP addresses; list 1 below > the other. > The second text file (TextIP-2.txt) contains 56 IP addresses; listed 1 below > the other. > > I am look for a script that would allow me to compare the content of these 2 > text files & identify those overlapping IP addresses that exist in BOTH of > these text files. > Maybe the results can be printed to a third text file. Any help appreciated. Perhaps you could refine your query to relate to the language you are currently working with. It would help if you posted whatever code you have written so far. -- Todd Vargo (Post questions to group only. Remove "z" to email personal messages) Hi JoJo,
> Folks: Maybe you're looking for something like this (PowerShell):> > > I have 2 separate text file each containing a bunch of IP addresses. > The first text file (TextIP-1.txt) contains 100 IP addresses; list 1 below > the other. > The second text file (TextIP-2.txt) contains 56 IP addresses; listed 1 below > the other. > param( [string] $File1='TextIP-1.txt', [string] $File2='TextIP-2.txt' ) $ips1=@{} cat $File1 | %{ $ip=[System.Net.IpAddress]::Parse($_); $ips1[$ip.Address]=$ip } $ips2=@{} cat $File2 | %{ $ip=[System.Net.IpAddress]::Parse($_); $ips2[$ip.Address]=$ip } foreach($ip1 in $ips1.Values) { if($ips2.Contains($ip1.Address)) { "$ip1 is in both files" } else { "$ip1 is in file '$File1' only" } } foreach($ip2 in $ips2.Values) { if(!$ips1.Contains($ip2.Address)) { "$ip2 is in file '$File2' only" } } hth Max
Show quote
<JoJo> wrote in news:e1522sciHHA.208@TK2MSFTNGP05.phx.gbl: It's an external program, not a script - but you could try the uniq > Folks: > > > I have 2 separate text file each containing a bunch of IP addresses. > The first text file (TextIP-1.txt) contains 100 IP addresses; list 1 > below the other. > The second text file (TextIP-2.txt) contains 56 IP addresses; listed 1 > below the other. > > I am look for a script that would allow me to compare the content of > these 2 text files & identify those overlapping IP addresses that > exist in BOTH of these text files. > Maybe the results can be printed to a third text file. Any help > appreciated. > > > > Thanks in advance. > Jo. > > > program available at http://lcpx07.lc.ehu.es/jma/win95.html (don't let the win95 scare you off - it runs under newer OS's, too) It will list either unique lines, or duplicate lines. I can't remember, but you may need to pipe the files through sort first. There may be other versions of this around, in some unix-like commandline utility collections. JoJo wrote:
Show quote > Folks: Lol, that lot of different codings where a script isn't needed at all ;-)> > > I have 2 separate text file each containing a bunch of IP addresses. > The first text file (TextIP-1.txt) contains 100 IP addresses; list 1 below > the other. > The second text file (TextIP-2.txt) contains 56 IP addresses; listed 1 below > the other. > > I am look for a script that would allow me to compare the content of these 2 > text files & identify those overlapping IP addresses that exist in BOTH of > these text files. > Maybe the results can be printed to a third text file. Any help appreciated. > > findstr.exe /G:TextIP-1.txt TextIP-2.txt >TextIP-3.txt Is all you need. -- Greetings Matthias |
|||||||||||||||||||||||