Solved

Powershell script to compare two txt files and use 'replace' on the identified differences

Posted on 2016-08-28
8
82 Views
Last Modified: 2016-09-18
Hi all,

I got two text files where txt1 got :

server 1 drive 1
server1 drive 4
server2 drive1

txt2 got:

server1 drive1
server2 drive1
server2 drive 3
server1 drive4
server2 drive5

where i want to match the txt files and the differences in txt2 file should be marked as "does not exists" like below

server1 drive1
server2 drive1
server2 drive 3 does not exists
server1 drive4
server2 drive5 does not exists

thanks in advance!
0
Comment
Question by:sahs2016 rred
[X]
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
  • 4
  • 4
8 Comments
 
LVL 70

Expert Comment

by:Qlemo
ID: 41774306
$txt1 = Get-Content txt1.txt
Get-Content txt2.txt | % { $_ +  ' does not exists'*($txt1 -notcontains $_) | Out-File txt3.txt

Open in new window

1
 

Author Comment

by:sahs2016 rred
ID: 41775644
Thank you very much Qlemo, I tested and it work for now. I'll monitor it for few more days and let you know the progress. thanks again u made my day!

I was struggling with this for past few weeks and had almost given up. but you shed some light on this now

cheers!
0
 

Author Comment

by:sahs2016 rred
ID: 41775712
Hi Qlemo,

I noticed the Out-file to new txt3.txt file can I just use to update the txt2 file instead?

I tried Get-Content txt2.txt | % { $_ +  ' does not exists'*($txt1 -notcontains $_) | Out-File txt2.txt But the command fails to write any lines.

Please help here I tried
$repl = Get-Content txt2.txt | % { $_ +  ' does not exists'*($txt1 -notcontains $_)
$repl|out-file txt2.txt

no use as only the 'does not exists' lines are written to txt2 file. I want to retain other lines as well in same txt2 file like below:

server1 drive1
server2 drive1
server2 drive 3 does not exists
server1 drive4
server2 drive5 does not exists


Please help!
0
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
LVL 70

Accepted Solution

by:
Qlemo earned 500 total points
ID: 41775821
It is a bad idea for tests to use the same file for input and output, but the second try should work. We can shorten it to
$txt1 = Get-Content txt12.txt
(Get-Content txt2.txt) | % { $_ +  ' does not exist'*($txt1 -notcontains $_) | Out-File txt3.txt

Open in new window

The added parens force the file to get read in as a whole first, then then pipeline starts and immediately replaces the input file.
BTW, I've corrected the grammatical error of "exists" having to be "exist".
0
 

Author Comment

by:sahs2016 rred
ID: 41777478
thanks Qlemo
I tried below

$a=(Get-Content txt2.txt) | % { $_ +  ' does not exist'*($txt1 -notcontains $_) | Out-File txt3.txt

if($a)
{$a|out-file txt2.txt}

txt2.txt contains lines marked with 'd  n  e' and the matching lines with txt1. however, old history that marks with d n e is deleted. if I use -append the lines get multiplied.

but still your suggested command works to an agreed extent. thanks again

and thanks correcting the grammatical error on 'exist'
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 41777613
I'm not sure I understand. You can't use the same file twice, as that doubles the "d n e" for each line (making it "d n e d n e"). Maybe you should explain more?

The part you added does not make sense. Out-File does not create any output you can capture in a variable, so everything related to $a is nonsense.
0
 

Author Comment

by:sahs2016 rred
ID: 41793621
Hi Qlemo,

The {$a|out-file txt2.txt} does write the $a content to the tst2.txt file. however, older 'dne' lines are removed. my requirement fulfills here, to some extent, as the recently marked 'dne' lines with the lines in the txt2 are written.

yes you are correct it does double up...I am using 'unique' to remove repeated lines in txt2.

At the end you helped me a lot thanks again :)
0
 
LVL 70

Assisted Solution

by:Qlemo
Qlemo earned 500 total points
ID: 41794621
We can change the script to either skip those lines already having a "does not exist" entry (and just append those "old" findings to the resulting file), or remove "does not exist" from the line and recheck with that. But we have to do something, as "server2 drive3 does not exist" will never be found in txt1.txt, even if you add "server2 drive3" to txt1.

From logic I would tell ignoring the prior result, and just recheck everything, is the correct approach.
$txt1 = Get-Content txt1.txt
(Get-Content txt2.txt) | 
  % { $_ -replace ' does not exist' } |
  % { $_ +  ' does not exist'*($txt1 -notcontains $_) | Out-File txt3.txt

Open in new window

0

Featured Post

What, When and Where - Security Threats from Q1

Join Corey Nachreiner, CTO, and Marc Laliberte, Information Security Threat Analyst, on July 26th as they explore their key findings from the first quarter of 2017.

Question has a verified solution.

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

Utilizing an array to gracefully append to a list of EmailAddresses
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
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…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

627 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