|
|
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:
[Quoted Text] > 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
|
|
<JoJo> wrote in message news:e1522sciHHA.208[ at ]TK2MSFTNGP05.phx.gbl...
[Quoted Text] > 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.
Unfortunately, you crossposted this to groups of dissimilar languages. 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,
[Quoted Text] > 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. >
Maybe you're looking for something like this (PowerShell):
param( [string] $File1='TextIP-1.txt', [string] $File2='TextIP-2.txt' )
$ips1=[ at ]{} cat $File1 | %{ $ip=[System.Net.IpAddress]::Parse($_); $ips1[$ip.Address]=$ip }
$ips2=[ at ]{} 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
|
|
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.
"Michael Harris (MVP)" <mikhar.at.mvps.dot.org> wrote in message news:ek3qhLdiHHA.4984[ at ]TK2MSFTNGP06.phx.gbl...
[Quoted Text] > 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 > >
|
|
<JoJo> wrote in news:e1522sciHHA.208[ at ]TK2MSFTNGP05.phx.gbl:
[Quoted Text] > 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. > > >
It's an external program, not a script - but you could try the uniq 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.
|
|
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:
[Quoted Text] >Appreciate the suggestion. I saved your file as "Compare.VBS". Then I did >"wscript compare.vbs"
See below.
>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. [snip] >"Michael Harris (MVP)" wrote in message >news:ek3qhLdiHHA.4984[ at ]TK2MSFTNGP06.phx.gbl... [snip] >> A simple demo... [snip] >> ---------- cscript ---------- [snip]
Have you tried "CSCRIPT compare.vbs"?
-- Michael Bednarek http://mbednarek.com/ "POST NO BILLS"
|
|
JoJo wrote:
[Quoted Text] > 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. > >
Lol, that lot of different codings where a script isn't needed at all ;-)
findstr.exe /G:TextIP-1.txt TextIP-2.txt >TextIP-3.txt
Is all you need.
-- Greetings Matthias
|
|
Hi, new to Powershell, trying to figure out how to do sort /+n in ps. I figure it should be simple, but I cant get it. appreciate help. thanks robert
"JoJo" wrote:
[Quoted Text] > 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. > > > >
|
|
One way to do it would be to use the sort-object cmdlet:
Get-Content someFile.txt | sort-object [ at ]{Expression={$_.substring(2)}}
This gets each line of a file, takes a slice of the line starting a position 2 and uses that for the sort key.
Here's a blog entry that explains sort-object some more:
http://blogs.msdn.com/powershell/archive/2006/04/25/583261.aspx
-- Nigel Sharples [MSFT] Windows PowerShell Microsoft Corporation This posting is provided "AS IS" with no warranties, and confers no rights.
"Robert Allison" <Robert Allison[ at ]discussions.microsoft.com> wrote in message news:12129094-0041-4AA9-80E7-46CD99150142[ at ]microsoft.com...
[Quoted Text] > Hi, new to Powershell, trying to figure out how to do sort /+n in ps. I > figure it should be simple, but I cant get it. appreciate help. > thanks robert > > "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. >> >> >> >> Thanks in advance. >> Jo. >> >> >> >>
|
|
Thanx!! This works fine.
"Nigel Sharples" wrote:
[Quoted Text] > One way to do it would be to use the sort-object cmdlet: > > Get-Content someFile.txt | sort-object [ at ]{Expression={$_.substring(2)}} > > This gets each line of a file, takes a slice of the line starting a position > 2 and uses that for the sort key. > > Here's a blog entry that explains sort-object some more: > > http://blogs.msdn.com/powershell/archive/2006/04/25/583261.aspx> > -- > Nigel Sharples [MSFT] > Windows PowerShell > Microsoft Corporation > This posting is provided "AS IS" with no warranties, and confers no rights. > > "Robert Allison" <Robert Allison[ at ]discussions.microsoft.com> wrote in message > news:12129094-0041-4AA9-80E7-46CD99150142[ at ]microsoft.com... > > Hi, new to Powershell, trying to figure out how to do sort /+n in ps. I > > figure it should be simple, but I cant get it. appreciate help. > > thanks robert > > > > "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. > >> > >> > >> > >> Thanks in advance. > >> Jo. > >> > >> > >> > >> > >
|
|
|