Solved

Powershell: Compatre 2 files

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Why would I want to create a function for tracking messages? I am glad you asked. As with most monotonous/routine tasks, human error tends to creep in after doing the same task over and over again. By creating a function, you load the function once…
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
This video discusses moving either the default database or any database to a new volume.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

707 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