Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Powershell: Compatre 2 files

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

This article will help you understand what HashTables are and how to use them in PowerShell.
A procedure for exporting installed hotfix details of remote computers using powershell
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

828 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