简体   繁体   English

使用 Powershell,我如何导出和删除 csv 行,其中特定值*未在 *不同* csv 中找到*?

[英]Using Powershell, how can I export and delete csv rows, where a particular value is *not found* in a *different* csv?

I have two files.我有两个文件。 One is called allper.csv一种叫allper.csv

institutiongroup,studentid,iscomplete
institutionId=22343,123,FALSE
institutionId=22343,456,FALSE
institutionId=22343,789,FALSE

The other one is called actswithpersons.csv另一种叫做actswithpersons.csv

abc,123;456
def,456
ghi,123
jkl,123;456

Note : The actswithpersons.csv does not have headers - they are going to be added in later via an excel power query so don't want them in there now.注意:actswithpersons.csv 没有标题 - 它们将在稍后通过 excel 电源查询添加,所以现在不希望它们在那里。 The actswithpersons csv columns are delimited with commas - there are only two columns, and the second one contains multiple personids - again Excel will deal with this later. actwithpersons csv 列用逗号分隔 - 只有两列,第二列包含多个 personids - Excel 稍后将处理这个问题。

I want to remove all rows from allper.csv where the personid doesn't appear in actswithpersons.csv, and export them to another csv.我想从 allper.csv 中删除所有行,其中 personid 没有出现在actswithpersons.csv 中,并将它们导出到另一个 csv。 So in the desired outcome, allper.csv would look like this所以在期望的结果中, allper.csv 看起来像这样

institutiongroup,studentid,iscomplete
institutionId=22343,123,FALSE
institutionId=22343,456,FALSE

and the export.csv would look like this和 export.csv 看起来像这样

institutiongroup,studentid,iscomplete
institutionId=22343,789,FALSE

I've got as far as the below, which will put into the shell whether the personid is found in the actswithpersons.csv file.我已经得到了以下内容,它将放入 shell 是否在 actwithpersons.csv 文件中找到 personid。

$donestuff = (Get-Content .\ActsWithpersons.csv | ConvertFrom-Csv); $ids=(Import-Csv .\allper.csv);foreach($id in $ids.personid) {echo $id;if($donestuff -like "*$id*" )
{
   echo 'Contains String'
}
else
{
   echo 'Does not contain String'
}}

However, I'm not sure how to go the last step, and export & remove the unwanted rows from allper.csv但是,我不确定如何 go 最后一步,并从 allper.csv 中导出并删除不需要的行

I've tried (among many things)我试过(在很多事情中)

$donestuff = (Get-Content .\ActsWithpersons.csv | ConvertFrom-Csv);
Import-Csv .\allper.csv |
    Where-Object {$donestuff -notlike $_.personid} |
        Export-Csv -Path export.csv -NoTypeInformation

This took a really long time and left me with an empty csv.这花了很长时间,给我留下了一个空的 csv。 So, if you can give any guidance, please help.所以,如果你能提供任何指导,请帮助。

Since your actswithpersons.csv doesn't have headers, in order for you to import as csv, you can specify the -Header parameter in either Import-Csv or ConvertFrom-Csv ;由于您的actwithpersons.csv没有标题,为了让您导入为 csv,您可以在Import-CsvConvertFrom-Csv中指定-Header参数; with the former cmdlet being the better solution.以前的 cmdlet 是更好的解决方案。

With that said, you can use any header name for those 2 columns then filter by the given column name ( ID in this case ) after your import of allper.csv using Where-Object :话虽如此,您可以对这 2 列使用任何 header 名称,然后在使用Where-Object导入allper.csv后按给定的列名称(在本例中为 ID )进行过滤:

$awp = (Import-Csv -Path '.\actswithpersons.csv' -Header 'blah','ID').ID.Split(';')
Import-Csv -Path '.\allper.csv' | Where-Object -Property 'Studentid' -notin $awp

This should give you:这应该给你:

institutiongroup    studentid iscomplete
----------------    --------- ----------
institutionId=22343 789       FALSE    

If you're looking to do it with Get-Content you can split by the delimiters of , and ;如果你想用Get-Content来做,你可以用,;的分隔符来分割。 . . This should give you just a single row of values which you can then compare the entirety of variable ( $awp ) using the same filter as above which will give you the same results:这应该只为您提供一行值,然后您可以使用与上面相同的过滤器比较整个变量( $awp ),这将为您提供相同的结果:

$awp = (Get-Content -Path '.\actswithpersons.csv') -split ",|;" 
Import-Csv -Path '.\allper.csv' | Where-Object -Property 'Studentid' -notin $awp

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

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