编辑 CSV 文件 - 根据条件 VIA Powershell 删除整个相似值行

Editing CSV File - Delete entire similar values row based on condition VIA Powershell

当 A 列的值(即 DevID)等于 B 列的值(即 ProdID)时,需要删除 CSV 文件的整行

我的代码:

$MergedFile= Import-Csv "C:\Users\d-mansings\Desktop\Mergedata.csv"

 $MergeTable=@{} 
   foreach($line in $MergedFile){
    if(Compare-Object $line.DevID $line.ProdID){
    echo "Not Matched, Keep the file as it"}
    else{ echo "Row need to be deleted"}
        }

代替Row need to be deleted我需要一个删除整行的命令。

以下是我的 CSV 文件:

"DevID","ProdID","cfname"
"-----","-----","------"
"10201","10202","Risk ID"
"10202","20202","Issue ID"
"10203","20203","Dependency ID"
"10204","20204","Server ID"
"10205","20205","Parent Application ID"
"10206","20206","Application Service ID"
"10207","20207","Application Supportability"
"10208","20208","Application Compatibility"
"10300","20300","Application Status"
"10301","20302","Contact ID Type 2"
"10302","20302","Contact ID Type 3"
"10303","20303","Contact ID Type 4"
"10304","10304","Business Service Manager"
"10308","20308","Server Location Name:" 

您可以使用 Where-Object cmdlet 过滤 DevID 不等于 ProdID 的所有条目,并将结果通过管道传递给 Export-Csv cmdlet 以保存回 csv:

Import-Csv 'source.csv' | 
    Where { $_.DevID -ne $_.ProdID } | 
    Export-Csv -Path 'destination.csv' -NoTypeInformation

输出:

"DevID","ProdID","cfname"
"10201","10202","Risk ID"
"10202","20202","Issue ID"
"10203","20203","Dependency ID"
"10204","20204","Server ID"
"10205","20205","Parent Application ID"
"10206","20206","Application Service ID"
"10207","20207","Application Supportability"
"10208","20208","Application Compatibility"
"10300","20300","Application Status"
"10301","20302","Contact ID Type 2"
"10302","20302","Contact ID Type 3"
"10303","20303","Contact ID Type 4"
"10308","20308","Server Location Name:"