删除双键 ini-file PowerShell



我有一个ini文件:

number=1
problem=4
issue=2
test=7
number=2
problem=5
issue=3
test=8

具有重复的键但不同的值。

如何使用电源外壳删除每个双键?

我的最终 ini 文件应如下所示:

number=1
problem=4
issue=2
test=7

谢谢!

更新: 09/10/2018 (我很在假期对不起(

我终于成功了!它工作绝对完美,谢谢你们,特别感谢@Richard Siddaway。

@Theo你是对的。我错过了我的[部分],我添加了它们,并为我的第二个程序添加了另一部分,应该能够读取ini文件

所以这是我的代码:

#merge two files
$files = Get-ChildItem "D:all"
foreach ($file in $files) {
$name = dir $file.FullName | select -ExpandProperty Name
$ReferenceObject = Get-Content D:File.ini
$DifferenceObject = Get-Content $file.FullName
Compare-Object -ReferenceObject $ReferenceObject -DifferenceObject $DifferenceObject -PassThru | Out-File ('D:output' + $name)
# filter Date an Number
$fileContent = Get-Content ('D:output' + $name)
# iterate over lines
foreach($line in $fileContent) {
#add "=" to [section]
if($line.StartsWith('[')) {
$newline = ($line + '=')
$newline | Add-Content ('D:output2' + $name)
}
# filter lines beginning with 'Date, ...'
elseif((-not $line.StartsWith('Date')) -and (-not $line.StartsWith('Number')) -and (-not $line.StartsWith('Date2'))) {
# output lines to new file
$line | Add-Content ('D:output2' + $name)
}
}
#remove duplicates
$fileContent = Get-Content ('D:output2' + $name)
$unqkeys = [ordered]@{}
foreach ($line in $fileContent){
if ($line -ne '') {
$a = $line -split '='
if (-not $unqkeys[$($a[0].Trim())]){
$unqkeys += @{$a[0].Trim() = $a[1].Trim()}
}
}
}
$unqkeys.GetEnumerator() | 
foreach {
Out-File -FilePath ('D:output3' + $name) -InputObject "$($_.key)=$($_.value)" -Append 
}

# add file location to [section]
$fileContent = Get-Content ('D:output3' + $name)
foreach($line in $fileContent) {
# filter for section
if($line.StartsWith('[')) {
# output lines to new file 
$newline = $line -replace "[[]", "|" -replace "]=", "]"
$linemodif = ("[C:mypathFile.ini" + $newline)
$linemodif | Add-Content ('D:output4' + $name)
}else {
$line | Add-Content ('D:output4' + $name)
}
}
}
#Move ouput to solution
Get-ChildItem -Path "D:output4*.*" -Recurse | Move-Item -Destination "D:solution" -force
#delete every single output
Remove-Item -Path D:output*.*
Remove-Item -Path D:output2*.*
Remove-Item -Path D:output3*.*
Remove-Item -Path D:output4*.*

这是我合并后的 ini 文件:

[SECTION1]
Tester=5
Magnet=2
Number=3459353484
Date=01/01/2018 11:00:00
Problem=
Issue=
[SECTION2]
Progress=0
Tester=4
Magnet=1
Number=0
Date=01/01/1999

这是我的结果:

[C:mypathFile.ini|SECTION1]
Tester=5
Magnet=2
Problem=
Issue=
[C:mypathFile.ini|SECTION2]
Progress=0

正如你所看到的,它看起来仍然像@Paxz所说的那样"只是将一些随机片段粘贴在一起"。我已经完成了并且对它非常满意,因为它完成了它的意义,但我很清楚我的脚本不是很干净,也不是一个好的解决方案。特别是我的输出:/也许有人提示我下次改进?也许我可以以某种方式将每个输出都保存在变量中?我在我的部分中添加了一个"=",以便能够使用理查兹脚本......

试试这个

if (Test-Path -Path c:testnew.ini) {
Remove-Item -Path c:testnew.ini
}
$unqkeys = [ordered]@{}

$lines = Get-Content -Path C:testt1.ini 
foreach ($line in $lines){
if ($line -ne '') {
$a = $line -split '='
if (-not $unqkeys[$($a[0].Trim())]){
$unqkeys += @{$a[0].Trim() = $a[1].Trim()}
}
}
}
$unqkeys.GetEnumerator() | 
foreach {
Out-File -FilePath C:testnew.ini -InputObject "$($_.key)=$($_.value)" -Append 
}

Get-Content -Path C:testnew.ini

我正在使用有序哈希表来保存唯一键 - 在文件中找到的第一个键。最后将数据写回新文件。 您应该能够修改它以满足您的需求

最新更新