sahs2016 rred
asked on
Powershell script to compare two txt files and use 'replace' on the identified differences
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!
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!
ASKER
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!
I was struggling with this for past few weeks and had almost given up. but you shed some light on this now
cheers!
ASKER
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!
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!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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'
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'
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.
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.
ASKER
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 :)
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 :)
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Open in new window