gravity
gravity

Reputation: 2066

Compare-Object Output Format

I have two CSV files I need to compare. Both of which may (or may not) contain an additional delimited field (delimited via a "|" character), like so:

(new.csv)
Title,Key,Value
Jason,Son,Hair=Red|Eyes=Blue
James,Son,Hair=Brown|Eyes=Green
Ron,Father,Hair=Black
Susan,Mother,Hair=Black|Eyes=Brown|Dress=Green

(old.csv)
Title,Key,Value
Jason,Son,Hair=Purple|Eyes=Blue
James,Son,Hair=Brown|Eyes=Green
Ron,Father,Hair=Purple
Susan,Mother,Hair=Black|Eyes=Brown|Dress=Blue

My problem comes in when I attempt to compare the two files...

$fileNew = "new.csv"
$fileOld = "old.csv"
$fileDiffOutputFile = "diff.txt"

$csvNewLog = (Import-CSV ($fileNew))
$csvOldLog = (Import-CSV ($fileOld))

$varDifferences = Compare-Object $csvOldLog $csvNewLog -property Title,Value

$varDifferences | Group-Object -Property Title | % {New-Object -TypeName PSObject -Property @{ NewValue=($_.group[0].Value); Title=$_.name; OldValue=($_.group[1].Value) } } | Out-File $fileDiffOutputFile -Append

Resulting in this output:

(diff.txt)
OldValue                   Title                      NewValue                 
--------                   -----                      --------                 
Hair=Purple|Eyes=Blue      Jason                      Hair=Red|Eyes=Blue       
Hair=Purple                Ron                        Hair=Black               
Hair=Black|Eyes=Brown|D... Susan                      Hair=Black|Eyes=Brown|...

Some of the values are inevitably going to extend out past the max length of the column, as it does with Susan above.

So, my question could have a couple of solutions that I can think of:

  1. Is there an easier way to isolate the values so that I only pull out the changed values, and not the entire string of delimited values?
  2. If not, is it possible to get the format to show the entire string (including the unchanged values part of the delimited string) instead?

Upvotes: 1

Views: 1185

Answers (1)

restless1987
restless1987

Reputation: 1598

If you include a format-table -wrap in your last line, like so?

$fileNew = "new.csv"
$fileOld = "old.csv"
$fileDiffOutputFile = "diff.txt"

$csvNewLog = (Import-CSV ($fileNew))
$csvOldLog = (Import-CSV ($fileOld))

$varDifferences = Compare-Object $csvOldLog $csvNewLog -property Title,Value

$varDifferences | Group-Object -Property Title | % {New-Object -TypeName PSObject -Property @{ NewValue=($_.group[0].Value); Title=$_.name; OldValue=($_.group[1].Value) } } | Format-Table -wrap | Out-File $fileDiffOutputFile -Append

Upvotes: 1

Related Questions