比较对象输出格式 [英] Compare-Object Output Format

查看:51
本文介绍了比较对象输出格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个CSV文件需要比较.两者都可以(也可以不)包含一个附加的分隔字段(通过"|"字符分隔),如下所示:

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

产生以下输出:

(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|...

某些值不可避免地会超出列的最大长度,就像上面的Susan一样.

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. 是否有一种更简便的方法来隔离值,以便我只提取更改后的值,而不提取整个带分隔符的值的字符串?
  2. 如果不是,是否可以获取显示整个字符串的格式(包括定界字符串中未更改的值部分)?

推荐答案

如果在最后一行中包含 format-table -wrap ,像这样吗?

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

这篇关于比较对象输出格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆