Solved

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

Posted on 2016-08-28
8
63 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
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
 
LVL 68

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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

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 68

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 68

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
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…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

920 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

13 Experts available now in Live!

Get 1:1 Help Now