Solved

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

Posted on 2016-08-28
8
59 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
  • 4
  • 4
8 Comments
 
LVL 68

Expert Comment

by:Qlemo
Comment Utility
$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
Comment Utility
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
Comment Utility
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
 
LVL 68

Accepted Solution

by:
Qlemo earned 500 total points
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:sahs2016 rred
Comment Utility
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 68

Expert Comment

by:Qlemo
Comment Utility
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
Comment Utility
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 68

Assisted Solution

by:Qlemo
Qlemo earned 500 total points
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This article will help you understand what HashTables are and how to use them in PowerShell.
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 …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
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…

763 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

7 Experts available now in Live!

Get 1:1 Help Now