[英]Powershell output information by heading variable from CSV
I'm trying to take a csv file with a couple of headings and format it into a nice word document while sorting alphabetically by heading1.我正在尝试获取一个带有几个标题的 csv 文件并将其格式化为一个不错的 word 文档,同时按标题 1 的字母顺序排序。
The csv looks like: csv 看起来像:
heading1,heading2,heading3, - up to heading5
info1,info2....info5
info1,info2....info5
etc etc
Heading1 has a lot of double-ups so the script will look something like: If info1 is the same as the above info1, just print info2 and info3. Heading1 有很多重复,所以脚本看起来像: 如果 info1 与上面的 info1 相同,只需打印 info2 和 info3。 Else print info 1-3 The problem is I don't understand how to assign variables to the headings in my ForEach loops. Else print info 1-3 问题是我不明白如何将变量分配给我的 ForEach 循环中的标题。
Here is what I've tried:这是我尝试过的:
$previousheading1 = ""
$FormatCSV = Import-Csv -Path $RawCSV.Filename -Header "Heading1", "Heading2", "Heading3", "Heading4", "Heading4" | Sort-Object -Property Heading1
$Heading1= $FormatCSV | Select-Object -ExpandProperty Heading1
$Heading2= $FormatCSV | Select-Object -ExpandProperty Heading2
$heading3= $FormatCSV | Select-Object -ExpandProperty Heading3
$FormatCSV = ForEach-Object {
if ($heading1 -eq $previousHeading1) {
Write-Output ("$Heading2- $Heading3")
Write-Output ("`v")
}
else {
Write-Output ("`v")
Write-Output ("$Heading1")
Write-Output ("$Heading2- $Heading3")
}
$previousHeading1 = ($Heading1)
}
At the moment I'm not getting any output in the terminal so something's clearly going wrong.目前我没有在终端中得到任何输出,所以显然出了点问题。
I have also tried:我也试过:
$FormatCSV = Import-Csv -Path $RawCSV.Filename -Header "heading1", "heading2", "heading3", "heading4", "heading5" | Sort-Object -Property tenant
$FormatCSV #This just shows that each entry has been sorted correctly into the nice four lines
Write-Output "The list should be below this" #This is only for the sake of my sanity
$previousHeading1 = ""
foreach ($FormatCSV in $FormatCSV.heading1) {
if ($FormatCSV.heading1-eq $previousHeading1) {
Write-Output ($FormatCSV.heading2, $FormatCSV.heading3)
}
else {
Write-Output ($FormatCSV.heading1)
Write-Output ($FormatCSV.heading2, $FormatCSV.heading3)
}
$previousHeading1 = ($FormatCSV.heading1)
}
The second line ($FormatCSV) I left in so I can see how the data is being organised in the terminal.我留下的第二行 ($FormatCSV) 以便我可以看到数据在终端中是如何组织的。 I have the feeling this is a simple fix and I've missed it.我觉得这是一个简单的修复,我错过了它。 Thanks in advance for the help :)在此先感谢您的帮助 :)
If it help, I'm using visual basic studio and running this in powershell.如果有帮助,我正在使用 Visual Basic Studio 并在 Powershell 中运行它。
Your first attempt was pretty close - I've made a few changes as follows:您的第一次尝试非常接近 - 我进行了一些更改,如下所示:
$csv = @"
heading1,heading2,heading3,heading4,heading5
info11,info12,info13,info14,info15
info21,info22,info23,info24,info25
info21,info32,info33,info34,info35
info41,info42,info43,info44,info45
"@
$data = $csv | ConvertFrom-Csv | Sort-Object -Property heading1
$previousheading1 = ""
$data | ForEach-Object {
if ($_.heading1 -eq $previousHeading1) {
Write-host "`t$($_.heading2)`t$($_.heading3)"
}
else {
Write-host "$($_.heading1)`t$($_.heading2)`t$($_.heading3)"
}
$previousHeading1 = $_.heading1
}
output:输出:
info11 info12 info13
info21 info22 info23
info32 info33
info41 info42 info43
I'll leave generating the Word Document as an exercise for the reader :-)我将把生成 Word 文档作为练习留给读者:-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.