【发布时间】:2012-06-22 17:58:23
【问题描述】:
我想检查 .csv 数据库的“代码”属性的值。如果“代码”字段的前三个字符等于 (-eq) 到“产品”的前三个字符,则显示结果数(计数)并将输入添加到 .csv 数据库。
但是,计数并没有显示为结果为空。
foreach ($row in $InventoryContents)
{
#Lets build a String first
$exportString = $row.User +','+$row.Product+','+$row.Company+','+$row.Model+',';
#Everything right so far
#first three characters of Product
$firstKey = $row.Product.SubString(0,3).ToUpper()
echo $firstKey;
#problem here
$nonSortedContents = Import-Csv $nonSortedCsv | where-Object { ($_.Code.split('-')[0]) -eq $firstKey }
#but arrapently this works : Import-Csv $nonSortedCsv| where-Object { ($_.Code.split('-')[0]) -eq "MON"}
$lines = $nonSortedContents.count
echo $lines
#null
#if not enetered
if ($lines -eq $null)
{
$wholeKey = $firstKey +'-'+ ("{0:D5}" -f 0).ToString()
$exportString = $exportString + $wholeKey
$exportString -join "," >> $nonSortedCsv
}
else
{
$wholeKey = $firstKey +'-'+ ("{0:D5}" -f $lines).ToString()
$exportString = $exportString + $wholeKey
$exportString -join "," >> $nonSortedCsv
}
}
我可以使用它
$nonSortedContents = @(Import-Csv $nonSortedCsv | where-Object { ($_.Code.split('-')[0]) -eq $firstKey })
显然,当 CSV 文件中只有一行时,Import-Csv 返回一个 PS自定义对象。当多于一行时,返回的对象是一个 对象[]。
【问题讨论】:
-
谢谢。遇到同样的问题。
标签: string powershell csv