Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 55
  • Last Modified:

Powershell: Compatre 2 files

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
Parity123
Asked:
Parity123
  • 2
  • 2
1 Solution
 
oBdACommented:
$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
 
Parity123Author Commented:
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
 
oBdACommented:
$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
 
Parity123Author Commented:
Thanks
0

Featured Post

 The Evil-ution of Network Security Threats

What are the hacks that forever changed the security industry? To answer that question, we created an exciting new eBook that takes you on a trip through hacking history. It explores the top hacks from the 80s to 2010s, why they mattered, and how the security industry responded.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now