2

我正在尝试编写这个 PS 脚本,但如果有人击败我,我相信他们会得到免费的因果报应。

无论如何,这就是我想要进行这样的文件设置

foo.bar=Some random text is stored here
foo.bar=Lazy maintainers make me angry
bar.foo=Hello World!
bar.foo=Hello World!

主要目标是删除任何重复的条目,我有几个 . . . 这似乎很容易

Get-Content c:\list.txt | Select-Object -Unique 

但我也想将具有相同密钥标识符的任何冲突存储到一个单独的文件中,以便我可以查看应该保留哪些冲突。

我仍然是一个 PS 新手,还没有找到一个好的方法来做到这一点。

4

1 回答 1

3

您可以使用Group-Object相同的键将项目组合在一起。然后查找其中包含多个元素的组(表示重复条目)。最后,将它们打印到某个文件中:

# raw content
$lines = Get-Content C:\data.txt

# package each line into a little object with properties Key and Val
$data = $lines |%{ $key,$val = $_.Split('='); new-object psobject -prop @{Key = $key; Val = $val} }

# group the objects by key, only keep groups with more than 1 element
$duplicates = $data | group Key |?{$_.Count -gt 1}

# print out each key and the different values it has been given
$duplicates |%{ "--- [$($_.Name)] ---"; $_.Group | select -expand Val }

结果:

--- [foo.bar] ---
Some random text is stored here
Lazy maintainers make me angry
--- [bar.foo] ---
Hello World!
Hello World!

Out-File如果您想将其存储在日志中,您可以将其传输到。

于 2012-08-30T15:51:40.507 回答