从哈希表中替换 CSV 中特定列中的值
Replace value in specific column in CSV from hashtable
我有一个导入的 CSV ($csv
),其中有多个 headers,其中之一是 "Target Server"。在 CSV 中,此列具有值 device1、device2 等
我还有一个哈希表 ($hash
),其中包含 name/value 对名称 (device1) - 值 (fqdn1) 等
所以我想用哈希表中的正确值替换 CSV 中的 "device1",例如:
foreach($row in $csv)
if($hash[$_.Name]){
$row."Target Server"= $hash[$_.Value]
}
我暖和了吗?
使用 ContainsKey()
方法查看哈希表是否包含具有特定 name/key:
的条目
foreach($row in $csv) {
if($hash.ContainsKey($row.'Target Server')) {
$row.'Target Server' = $hash[$row.'Target Server']
}
}
我有一个导入的 CSV ($csv
),其中有多个 headers,其中之一是 "Target Server"。在 CSV 中,此列具有值 device1、device2 等
我还有一个哈希表 ($hash
),其中包含 name/value 对名称 (device1) - 值 (fqdn1) 等
所以我想用哈希表中的正确值替换 CSV 中的 "device1",例如:
foreach($row in $csv)
if($hash[$_.Name]){
$row."Target Server"= $hash[$_.Value]
}
我暖和了吗?
使用 ContainsKey()
方法查看哈希表是否包含具有特定 name/key:
foreach($row in $csv) {
if($hash.ContainsKey($row.'Target Server')) {
$row.'Target Server' = $hash[$row.'Target Server']
}
}