编辑CSV文件-根据条件VIA Powershell删除整个相似值行 [英] Editing CSV File - Delete entire similar values row based on condition VIA Powershell

查看:67
本文介绍了编辑CSV文件-根据条件VIA Powershell删除整个相似值行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当列A的值(即 DevID )等于列B的值(即 ProdID CSV 文件的整行.代码>)

Need to delete an entire row of a CSV file, when value of column A (i.e. DevID) equals value of column B (i.e. 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"}
        }

代替行需要删除,我需要一个命令来删除整行.

In place of Row need to be deleted I need a command that would delete an entire row.

以下是我的 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保存回去:

You can use the Where-Object cmdlet to filter all entries where DevID not equals ProdID and pipe the result to the Export-Csv cmdlet to save the csv back:

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

这篇关于编辑CSV文件-根据条件VIA Powershell删除整个相似值行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆