【问题标题】:Powershell, csv match a row and column to modify another columnPowershell,csv匹配一行和一列修改另一列
【发布时间】:2019-10-30 15:51:40
【问题描述】:

我的问题很简单,我有一个格式如下的 csv 文件:

Client   Status       Goal       Project
A         Done         Done       Waiting
B         Finished     Done       Cancelled
C                      Waiting    Finished
D         Done         Done       Done

现在,我的问题很简单,例如,我想更新与客户“B”匹配的“目标”列上的值

我知道我可以执行 import-csv 并使用计算属性修改列,但我不知道如何修改与另一列中的另一行匹配的列上的特定行,这是我的问题。例如,我想将 Waiting 更改为 Finished 客户端 C,或将 canceling 更改为 Waiting on Client B。

【问题讨论】:

  • 我的例子中的错误,不是真正的标题。

标签: powershell csv


【解决方案1】:

你可以这样做:

$csv = Import-Csv 'D:\clientprojects.csv'
($csv | Where-Object {$_.Client -eq 'C'}).Goal = 'Finished'
($csv | Where-Object {$_.Client -eq 'B'}).Project = 'Waiting'

#output on console
$csv

# write to new CSV file
$csv | Export-Csv -Path 'D:\clientprojects-updated.csv' -NoTypeInformation

控制台输出:

Client Status   Goal     Project 
------ ------   ----     ------- 
A      Done     Done     Waiting 
B      Finished Done     Waiting 
C               Finished Finished
D      Done     Done     Done

【讨论】:

  • 谢谢,现在我知道 this 的语法了。 !
猜你喜欢
  • 2018-07-24
  • 1970-01-01
  • 2019-06-19
  • 1970-01-01
  • 1970-01-01
  • 2021-10-16
  • 2015-06-27
  • 1970-01-01
  • 2019-11-19
相关资源
最近更新 更多