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

时间:2016-07-20 11:10:03

标签: csv powershell export-to-csv delete-row

当A列的值(即CSV)等于B列的值(即DevID

时,需要删除ProdID文件的整行

我的代码:

$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:" 

1 个答案:

答案 0 :(得分:0)

您可以使用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:"