Powershell从csv计数相同的值 [英] Powershell counting same values from csv

查看:586
本文介绍了Powershell从csv计数相同的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用PowerShell,我可以导入CSV文件,并计算有多少对象等于a。例如,

Using PowerShell, I can import the CSV file and count how many objects are equal to "a". For example,

@(Import-csv location | where-Object{$_.id -eq "a"}).Count

有没有办法通过每一列和行寻找同一个字符串a添加到计数?或者,我必须对每个列重复执行相同的命令,只是使用不同的关键字?

Is there a way to go through every column and row looking for the same String "a" and adding onto count? Or do I have to do the same command over and over for every column, just with a different keyword?

推荐答案

所以我创建了一个包含5列人名的虚拟文件。现在告诉你这个过程将如何工作我将告诉你在任何字段中出现文字Ann的频率。

So I made a dummy file that contains 5 columns of people names. Now to show you how the process will work I will show you how often the text "Ann" appears in any field.

$file = "C:\temp\MOCK_DATA (3).csv"
gc $file | %{$_ -split ","} | Group-Object | Where-Object{$_.Name -like "Ann*"}

Count Name  Group                          
----- ----  -----                          
    5 Ann   {Ann, Ann, Ann, Ann...}        
    9 Anne  {Anne, Anne, Anne, Anne...}    
   12 Annie {Annie, Annie, Annie, Annie...}
   19 Anna  {Anna, Anna, Anna, Anna...} 

安出现5次自己。但是它也是其他名称的一部分。让我们使用一个简单的正则表达式来查找只有Ann的所有值。

"Ann" appears 5 times on it's own. However it is a part of other names as well. Lets use a simple regex to find all the values that are only "Ann".

(select-string -Path 'C:\temp\MOCK_DATA (3).csv' -Pattern "\bAnn\b" -AllMatches | Select-Object -ExpandProperty Matches).Count

这将返回5,因为 \b 是一个字边界。实质上,它只是看每一行之间的逗号或开始或结束。这省略了像Anna和Annie的结果,你可能有。 选择对象-ExpandProperty匹配是重要的,如果你有多个匹配在一行。

That will return 5 since \b is for a word boundary. In essence it is only looking at what is between commas or beginning or end of each line. This omits results like "Anna" and "Annie" that you might have. Select-Object -ExpandProperty Matches is important to have if you have more than one match on a single line.

小注意事项

不必担心,简单的是,您的标头可能与您要查找的值匹配。不可能这是为什么我不解释它。如果这是一种可能性,那么我们可以使用 Select -Skip 1 来替换 Get-Content

It should not matter but in trying to keep the code simple it is possible that your header could match with the value you are looking for. Not likely which is why I don't account for it. If that is a possibility then we could use Get-Content instead with a Select -Skip 1.

这篇关于Powershell从csv计数相同的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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