Solved

Powershell: Compatre 2 files

Posted on 2016-10-07
4
34 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 83

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 83

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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

The article will show you how you can maintain a simple logfile of all Startup and Shutdown events on Windows servers and desktops with PowerShell. The script can be easily adapted into doing more like gracefully silencing/updating your monitoring s…
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

895 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now