powershell - 删除双键ini文件powershell
问题描述
我有一个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 所说,它看起来仍然像某人“只是将一些随机片段粘贴在一起”。我已经完成并且对它非常满意,因为它完成了它的本意,但我很清楚我的脚本不是很干净,也不是一个好的解决方案。尤其是我的输出:/也许有人提示我下次改进?也许我可以以某种方式将每个输出保存在一个变量中?我在我的部分中添加了一个“=”,以便能够使用理查兹脚本......
解决方案
尝试这个
if (Test-Path -Path c:\test\new.ini) {
Remove-Item -Path c:\test\new.ini
}
$unqkeys = [ordered]@{}
$lines = Get-Content -Path C:\test\t1.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:\test\new.ini -InputObject "$($_.key)=$($_.value)" -Append
}
Get-Content -Path C:\test\new.ini
我正在使用有序哈希表来保存唯一键 - 在文件中找到的第一个键。最后将数据写回新文件。您应该能够修改它以满足您的需求
推荐阅读
- c++ - eigen:创建 Vector-like Replicate(使用一个索引访问,LinearAccessBit)
- python - Python - 从网页下载文件
- shell - 使用 shell 脚本打开 gedit,然后在关闭 gedit 后执行另一个进程
- c# - 用另一个替换 PartialView (ASP NET CORE MVC)
- django - 如何设置 django 模型字段值以根据不同模型中其他字段的值自动计算一个值
- c - 链表只打印最后添加的成员。可能是什么错误?
- dropzone.js - 使用 fileRename 选项后如何以编程方式更新 dropzone 预览文件名
- iframe - 是否有更简单的方法来为 iFrame 中呈现的操作切换布局?
- github - 如何查看 GitHub PR 最新更新中的所有(包括过期和已解决的)评论?
- javascript - 查找并移动 1 列中具有相同前缀的所有单元格 - 谷歌表 - 谷歌应用脚本