使用Powershell替换列值 [英] Replace column values with Powershell
问题描述
我试图循环通过csv,并将 Enabled
中的任何值替换为 True
A
。
I'm trying to cycle through a csv and replace any values in a column named Enabled
from True
to A
.
Import-Csv .\test.csv | Where-Object {$_.Enabled -eq 'True'} --> what goes here to replace 'True' with 'A'?
推荐答案
code>的行为类似于过滤器,因此传递到其余管道的列只会是
启用
的列 True
;这将阻止你包括在输出文件中的其他人(我假设你想有一个完整的文件在结尾)。
Where-Object
acts like a filter, so the columns that get passed to the rest of the pipeline will only be the ones where Enabled
is True
; which will prevent you from including the others in your output file (I'm assuming you want to have a complete file at the end).
所以我建议使用 ForEach-Object
,然后根据其中的条件修改,但仍然通过每个对象(修改或不修改):
So I would recommend using ForEach-Object
and then modifying based on a condition inside there, but still passing each object through (modified or not):
Import-Csv .\test.csv | ForEach-Object {
if ($_.Enabled -eq 'True') {
$_.Enabled = 'A'
}
$_
} | Export-Csv .\test-modified.csv -NoTypeInformation
这篇关于使用Powershell替换列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!