我想读取一个csv文件,根据两个字段的值对其进行过滤,并设置另一个字段的值。下面是我想要实现一个简单的例子 :Powershell:设置过滤数组的值
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.
任何意见,如何实现这一点,将不胜感激。
艾伦牛逼
注意自我:确保CSV中的标题行没有尾随空间......这使我在过去几分钟的测试中失去了... – Joey 2010-06-17 16:31:17
谢谢stej工作过。 – 2010-06-17 16:47:39