我有下面的csv
Total Devices Affected Comments
5 Host1.Domain
Host2.Domain
Host3.Domain
Host4.Domain
Host5.Domain
4 Host1.Domain
Host2.Domain
Host3.Domain
Host4.Domain
我希望能够从每个单元格中删除条目Host2.Domain和Host3.Domain而不删除整个单元格。我只使用Windows PowerShell。这可能吗?
$data = Import-Csv .\acas.csv
foreach($title in $data)
{
$totaldevices = $title."Total Devices Affected"
$comments = $title."Comments"
}
答案 0 :(得分:2)
假设csv中的Comments
字段是一个多行字符串,请按照以下简单for
循环执行此操作:
# Define the host names we want to remove
$PendingRemoval = "Host2.Domain","Host3.Domain"
# Import the original data set
$Data = Import-Csv .\original.csv
for($i = 0; $i -lt $data.Count; $i++)
{
# Grab all hostnames from the comments field
$Comments = $Data[$i].Comments -split "`r?`n"
# Filter out unwanted ones
$Comments = $Comments |Where-Object {$_ -notin $PendingRemoval}
# Update original row
$Data[$i].Comments = $Comments -join [System.Environment]::NewLine
}
# Export it to a new csv
$Data |Export-Csv .\updated.csv