【发布时间】:2011-03-05 01:24:48
【问题描述】:
我想读入一个 csv 文件,根据两个的值过滤它 字段并设置另一个字段的值。这是一个简单的例子 我想要达到的目标:
c:\somefile.csv 内容:
firstField,secondField,thirdField
1,2,"somevalue"
2,2,"avalue"
3,1,"somevalue"
#Import file into array
$csv = Import-Csv c:\somefile.csv
# Where secondField = 2 and thirdField = "someValue" set thirdField =
"anotherValue"
$csv | where {$_secondField -eq 2 -and $_.thirdField = "somevalue"} |
<set value of thirdField = "anotherValue">
我该怎么做。如您所见,从示例中,我可以阅读 并过滤数组。但我不知道如何设置 第三场。我尝试了 set-itemproperty 但收到错误:“ 管道后无法调用 WriteObject 和 WriteError 方法 已关闭”。
编辑:我也只想更改返回的前 2 个项目(行)的值。 我回答:我使用 Select -first 2。
任何关于如何实现这一点的建议将不胜感激。
艾伦·T
【问题讨论】:
标签: arrays powershell csv