简体   繁体   English

基于2列比较的Powershell删除行(csv文件)

[英]Powershell delete row (csv file) based on compare of 2 columns

I have 2 csv files neither is sorted: File1 structure = name,loc,date,url File2 structure = name,url 我有2个csv文件,均未排序:File1结构=名称,位置,日期,URL File2结构=名称,URL

I am trying to use powershell to remove any rows from File1 that contain the same name from File2. 我正在尝试使用Powershell从File1中删除任何包含File2中相同名称的行。

What I have is extremely slow based on the number of records I have and how many times this is repeated. 根据我拥有的记录数量以及重复多少次,我的拥有速度非常慢。
Is there a better way to do this? 有一个更好的方法吗?

$file1 = "file1.csv"
$file2 = "file2.csv"

$f1 = import-csv $file1
$f2 = import-csv $file2

$f1names = $f1.name
$f2names = $f2.name

$difference = compare-object -referenceobject $f1names -differenceobject $f2names -passthru

$results = $difference | % {
$f1 | ? { $f1.name -eq $_}
$f2 | ? { $f2.name -eq $_}
}

See if this isn't faster: 看看这不是更快:

$file1 = "file1.csv"
$file2 = "file2.csv"

$f2Names = import-csv $file2 | select -ExpandProperty Name

Import-Csv $file1 |
Where { $f2Names -notcontains $_.Name }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM