Author Posts

January 1, 2012 at 12:00 am

by ChrisL at 2013-01-03 16:28:00

I have a previous question which nohandle answered for me in which I was trying to do a line by line compare of two files and add data from the first to the second where it did not already exist.
viewtopic.php?f=2&t=923&p=3812

Now I'm trying to do the opposite, delete any data found in both files from the second. But no dice so far... Any suggestions?
I used write-host for $_ prior to ForEach-Object but got no output... so I'm assuming I need to modify my approach. I apologize in advance; I'm still relatively new to PS.


$copy = 'c:\temp\compare\copy.txt'
$orig = 'c:\temp\compare\orig.txt'
Compare-Object -ReferenceObject (get-Content $orig) -DifferenceObject (get-Content $copy) | where {$_.sideIndicator -eq "=="} | select -ExpandProperty InputObject | ForEach-Object {$_ -replace "$_", ""} | Set-Content $copy

by DonJ at 2013-01-03 16:50:44

I think you'd be a lot happier if you didn't try to do this as a giant one-liner. It'd give you more opportunities to put some debug code in and see what was in the pipeline at each step.

But, let's follow the logic. Compare-Object produces difference objects. Your Where-Object command looks fine. So long as you don't select a property, which you didn't, the difference object should have an InputObject property. I'd start by ending the command line after Select-Object. What's that output look like?

by nohandle at 2013-01-04 08:31:19

Foreach {$_ -replace "$_", ""}
wut?

The linked answer should be capable of doing what you need just by changing the side indicator value in the where clause.

by ChrisL at 2013-01-04 10:46:34

[quote="DonJ"]I think you'd be a lot happier if you didn't try to do this as a giant one-liner. It'd give you more opportunities to put some debug code in and see what was in the pipeline at each step.[/quote]
I was thinking aboutthat as I posted this... That I should expand it out and try and compress it later (if possible or neccessary).

[quote="DonJ"]But, let's follow the logic. Compare-Object produces difference objects. Your Where-Object command looks fine. So long as you don't select a property, which you didn't, the difference object should have an InputObject property. I'd start by ending the command line after Select-Object. What's that output look like?[/quote]
The file itself is unchanged, and when I write-host the pipeline object ($_) at that point I get no output.

[quote="nohandle"]Foreach {$_ -replace "$_", ""}
wut?

The linked answer should be capable of doing what you need just by changing the side indicator value in the where clause.[/quote]
That was the first thing I did but it didn't work; and I know that line looks funky, I'd tried that after * didn't work before I realized I wasn't getting a pipeline object

by nohandle at 2013-01-04 12:16:25

Got it, you are missing -IncludeEqual 🙂

by ChrisL at 2013-01-04 20:54:22

That helped; but now it returns an invalid regular expression pattern on the asterisk:

Compare-Object -ReferenceObject (get-Content $orig) -DifferenceObject (get-Content $copy) -includeequal | where {$_.sideIndicator -eq "=="} | select -ExpandProperty InputObject | ForEach-Object {$_ -replace "*", ""} | Set-Content $copy

by nohandle at 2013-01-05 02:14:44

I've re-read your request.
[quote="ChrisL"]Now I'm trying to do the opposite, delete any data found in both files from the second.[/quote]
If by the second you mean the difference object you should be outputting all the entries marked '=>' then you get only the lines that are not present in the reference file.

by nohandle at 2013-01-05 02:15:44

And ommit the foreach {-replace } of course.:)

by ChrisL at 2013-01-07 09:59:54

[quote="nohandle"]I've re-read your request.
[quote="ChrisL"]Now I'm trying to do the opposite, delete any data found in both files from the second.[/quote]
If by the second you mean the difference object you should be outputting all the entries marked '=>' then you get only the lines that are not present in the reference file.[/quote]

I see what you mean, not try and delete lines but re-write the file with only the data I want... I'll give it a go.

by ChrisL at 2013-01-07 11:16:43

Awesome, that worked; thanks soooo much!