Solved

Powershell: Compatre 2 files

Posted on 2016-10-07
4
41 Views
Last Modified: 2016-10-07
Hello Experts,

I have 2 files. File1 has 2 columns, A, B and File 2 has 1 Column A. I want to check for values in File2 if it exists is File1, and output the matching line. Could you please assist with a script.

For instance:

File1

Computer1, 1.2.3.4
Computer2, 2.3.4.5

File2

2.3.4.5

The result should be
Compuer2,2.3.4.5

Thanks.
0
Comment
Question by:Parity123
  • 2
  • 2
4 Comments
 
LVL 84

Expert Comment

by:oBdA
ID: 41834193
$File2 = Get-Content -Path "C:\Temp\File2.txt"
Get-Content -Path "C:\Temp\File1.txt" | Where-Object {$File2 -contains $_.Split(',')[1].Trim()}

Open in new window

0
 

Author Comment

by:Parity123
ID: 41834205
Thanks oBdA. Could you please help me with outputting the ones that don't match as well from file2.

For instance:
File1

 Computer1, 1.2.3.4
 Computer2, 2.3.4.5

File2

2.3.4.5
0.2.3.4

Not matching file should contain 0.2.3.4,
and the matching file should contain

Computer2,2.3.4.5
0
 
LVL 84

Accepted Solution

by:
oBdA earned 500 total points
ID: 41834219
$File1 = Get-Content -Path "C:\Temp\File1.txt"
$File2 = Get-Content -Path "C:\Temp\File2.txt"
$File1 | Where-Object {$File2 -contains $_.Split(',')[1].Trim()} | Set-Content -Path "C:\Temp\Match.txt"
$IP1 = $File1 | % {$_.Split(',')[1].Trim()}
$File2 | Where-Object {$IP1 -notcontains $_} | Set-Content -Path "C:\Temp\NoMatch.txt"

Open in new window

0
 

Author Closing Comment

by:Parity123
ID: 41834304
Thanks
0

Featured Post

Free Webinar: AWS Backup & DR

Join our upcoming webinar with experts from AWS, CloudBerry Lab, and the Town of Edgartown IT to discuss best practices for simplifying online backup management and cutting costs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question