Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Comparing two text files - outputting whole line that contains match?

$
0
0

I have two text files, one text file has a list of IP addresses one per line, the other text file has a list of IP addresses and their corresponding hostname one combo per line. So as below...

Text1:

103.14.120.121
103.16.26.228
103.16.26.36
103.19.89.118
103.19.89.55
103.224.167.121
68.67.176.14
204.11.109.78

Text2:

86 Internap Network Services Corporation    74.217.34.2                      
88 Internap Network Services Corporation    70.42.33.241                     
88 Aggregate Knowledge Inc.    8.15.229.36                                   
88 Exponential Interactive, Inc.    204.11.109.78                            
88 Savvis    205.217.169.112                                                  
89 AppNexus, Inc    68.67.176.14                                             
89 DoubleVerify, Inc.    204.154.111.227                                     
89 SoftLayer Technologies Inc.    72.14.184.156 

So if any IP addresses in Text1 show up in Text2, then I want the whole line in Text2 to be output to results.txt duplicate(more than one) matches is ok. What I have below is not working.

Results.txt

89 AppNexus, Inc    68.67.176.14
88 Exponential Interactive, Inc.    204.11.109.78

 

Powershell Version 2.0

 $malip = (Get-content "C:\Users\admin\Desktop\Analytics\Destination_IP_Module\temp\text1.txt")
Get-Content "C:\Users\admin\Desktop\Analytics\Destination_IP_Module\temp\text2.txt" |
select-string -pattern $malip  -Allmatches |
sort | Out-File "C:\Users\admin\Desktop\Analytics\Destination_IP_Module\temp\results.txt" -width 4096

I 've made a few different scripts now, I can do simple things, but this one is over my head. Any help is much appreciated, thanks.


Viewing all articles
Browse latest Browse all 6937

Trending Articles