合并CSV文件powershell

时间:2012-05-21 15:43:32

标签: variables powershell csv

所以这是背景故事,我试图以几种不同的方式验证数据。我查询了一个数据库并将该数据导出到csv。

然后使用powershell我运行以下命令

$staleDates = Import-Csv DataAudit.csv | Where-Object { $_.asOfDate -ne "" } | Foreach-Object { $_.AsOfDate = $_.AsOfDate -as [datetime]; $_ } | Where-Object {$_.asOfDate -lt $measuredDate}

$nullDates = Import-Csv DataAudit.csv | Where-Object {$_.asOfDate -eq ""}

$percentTooLarge = Import-Csv DataAudit.csv | Foreach-Object { $_.SumHoldingPercent = $_.SumHoldingPercent -as [float]; $_ } | Where-Object { $_.SumHoldingPercent -gt 100 }

$percentTooSmall = Import-Csv DataAudit.csv | Where-Object {$_.SumHoldingPercent -ne "" } | Foreach-Object { $_.SumHoldingPercent = $_.SumHoldingPercent -as [float]; $_ } | Where-Object {$_.SumHoldingPercent -lt 99.99999 }

有没有办法可以将这4个变量的结果合并到一个单独的csv中,然后我可以通过电子邮件将其作为坏数据的报告?

另外,我正在使用Powershell v1.0

1 个答案:

答案 0 :(得分:2)

以下内容应该有效:

$badData = Import-Csv DataAudit.csv | Where-Object { $_.asOfDate -ne "" } | Foreach-Object { $_.AsOfDate = $_.AsOfDate -as [datetime]; $_ } | Where-Object {$_.asOfDate -lt $measuredDate}
$badData += Import-Csv DataAudit.csv | Where-Object {$_.asOfDate -eq ""}
$badData += Import-Csv DataAudit.csv | Foreach-Object { $_.SumHoldingPercent = $_.SumHoldingPercent -as [float]; $_ } | Where-Object { $_.SumHoldingPercent -gt 100 }
$badData += Import-Csv DataAudit.csv | Where-Object {$_.SumHoldingPercent -ne "" } | Foreach-Object { $_.SumHoldingPercent = $_.SumHoldingPercent -as [float]; $_ } | Where-Object {$_.SumHoldingPercent -lt 99.99999 }

$badData | Export-CSV -NoTypeInformation -Path allBadData.csv