2016-07-20 2 views
1

VIA 조건에 따라 전체 비슷한 값 행을 삭제 내 코드 :편집 CSV 파일은 - (즉 <code>ProdID</code>)</p> <p>열 A (즉, <code>DevID</code>)의 값이 열 B의 값과 동일 할 때, <code>CSV</code> 파일의 전체 행을 삭제해야 파워 쉘

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

[email protected]{} 
    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:" 

답변

0

DevID하지 다시 CSV 저장 Export-Csv cmdlet으로 ProdID 및 파이프 결과와 동일한 경우 당신은 모든 항목을 필터링 할 Where-Object cmdlet을 사용할 수 있습니다

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:" 
+1

Worked! 감사합니다. 현재 답변으로 표시 할 수 없으며 허가를받는대로 처리 할 수 ​​있습니다. – Abhaya