动态循环遍历文本文件并获得常见匹配项

Dynamically loop through a text file and get common matches

我有一个将数据输出到文本文件的脚本。现在我需要一种方法来计算与项目匹配的行数并进行计数。我知道如何通过

获得一个值的计数
$ProjectMatches = Select-String -InputObject $FileContent -Pattern "PROJECT ID FOAH834" -AllMatches

但我需要遍历文本文件并为每个 PROJECT ID SOMEPROJECTNUMBER 获取计数并获取每个的计数并将这些结果导出到 csv 文件。我的目标是展示每个项目投入了多少时间。每个条目是 15 分钟的时间。

11/2/2017 8:07:34 PM
This 15 minutes is dedicated to PROJECT ID FOAH834
11/2/2017 8:07:36 PM
This 15 minutes is dedicated to PROJECT ID FOAH834
11/2/2017 8:07:38 PM
This 15 minutes is dedicated to PROJECT ID FOAH841
11/2/2017 8:10:15 PM
This 15 minutes is dedicated to PROJECT ID FOAH18
11/2/2017 8:10:17 PM
This 15 minutes is dedicated to PROJECT ID FOAH690
11/2/2017 8:10:21 PM
This 15 minutes is dedicated to PROJECT ID PRJ0036909
11/2/2017 8:19:32 PM
This 15 minutes is dedicated to PROJECT ID FOAH18
11/2/2017 8:21:02 PM

您可能想要进行一些正则表达式匹配,然后进行字符串操作,而不是对所有值进行硬编码

我会使用类似

的东西
$matches = [regex]:matches($FileContent,"PROJECT ID .*<br/>")

获取所有项目代码。然后遍历它,进行一些字符串操作并将其插入哈希 table

$hashtable = @{}
foreach ($entry in $matches){
    $entry = $entry.replace(...)
    $projectcode = $entry.split(....)
    $hashtable[$projectcode] += 1
}

然后你可以用类似

的东西处理散列table
foreach($entry in $hashtable.keys){
    write-output "$entry: $($hashtable[$entry])"
}