【发布时间】:2019-07-08 12:53:42
【问题描述】:
我提供了一个文本文件中的字符串块列表,我需要将它放在 powershell 中的数组中。
列表如下所示
a:1
b:2
c:3
d:
e:5
[blank line]
a:10
b:20
c:30
d:
e:50
[blank line]
...
我希望它在一个 powershell 数组中进一步使用它。
我正在使用
$output = @()
Get-Content ".\Input.txt" | ForEach-Object {
$splitline = ($_).Split(":")
if($splitline.Count -eq 2) {
if($splitline[0] -eq "a") {
#Write-Output "New Block starting"
$output += ($string)
$string = "$($splitline[1])"
} else {
$string += ",$($splitline[1])"
}
}
}
Write-Host $output -ForegroundColor Green
$output | Export-Csv ".\Output.csv" -NoTypeInformation
$output | Out-File ".\Output.txt"
但这整件事感觉很麻烦,而且输出不是 csv 文件,在这一点上我认为是因为我使用数组的方式。 Out-File 确实会生成一个文件,其中包含用逗号分隔的行。
也许有人可以推动我朝着正确的方向前进。
谢谢 x
【问题讨论】:
-
如果你需要一个 csv 文件,为什么要先将其转换为数组??无论如何,如果你需要一个 csv,你需要添加一个标题并将其转换回文本......然后导出到 csv ...
标签: list powershell file text