【发布时间】:2019-01-26 15:23:36
【问题描述】:
我有一个ini文件:
number=1
problem=4
issue=2
test=7
number=2
problem=5
issue=3
test=8
键重复但值不同。
如何使用 powershell 删除每个双键?
我的最终 ini 文件应如下所示:
number=1
problem=4
issue=2
test=7
谢谢!
更新:2018 年 9 月 10 日(抱歉,我在假期)
我终于成功了!它绝对完美,谢谢你们,特别感谢@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:\mypath\File.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:\mypath\File.ini|SECTION1]
Tester=5
Magnet=2
Problem=
Issue=
[C:\mypath\File.ini|SECTION2]
Progress=0
如您所见,正如@Paxz 所说,它看起来仍然像某人“只是将一些随机的 sn-ps 粘贴在一起”。我已经完成并且对它非常满意,因为它完成了它的本意,但我很清楚我的脚本不是很干净,也不是一个好的解决方案。尤其是我的输出:/也许有人提示我下次改进?也许我可以以某种方式将每个输出保存在一个变量中?我在我的部分中添加了一个“=”,以便能够使用 Richards 脚本...
【问题讨论】:
-
到目前为止你尝试过什么?我们很乐意帮助您调试脚本,但不会从头开始为您编写。
-
到目前为止你尝试过什么?你在哪里失败了?我们不是来为您编写代码的。请尝试提供minimal reproducible example。
-
当然,等一下
-
我将其标记为正确的 ty。我添加了我的代码。你知道删除双键的命令吗?
-
好吧,您的代码看起来就像您只是将早期答案中的一些随机 sn-ps 粘贴在一起......但总而言之,它看起来很像一个 XY 问题 (xyproblem.info)。请尝试解释您实际尝试实现的目标并尝试解释您的情况,为什么要删除文件的重复行?为什么你的 .ini 文件甚至有重复的行?
标签: powershell duplicates ini