PowerShell,计算文本文件中字符串出现的次数

3
我有一个文本文件,其格式如下:
Lorem Ipsum Lorem Ipsum Ipsum user:john
Lorem Ipsum user:peter
Lorem Ipsum Lorem Ipsum user:george
Lorem Ipsum user:john
Lorem Ipsum  vLorem Ipsum user:george
Lorem Ipsum user:john

我需要在PowerShell V2上开发一个脚本,用于统计出现次数并生成以下内容的CSV文件,
john,3
george,2
peter,1

我计划循环遍历文件,将每个用户保存在数组中,然后使用get-content和模式来计算出现次数,例如:

#assumming i was able to fill the array in some way :)
$users =@('john','peter', 'george')
for each ($user in $users)
{
     $count = get-content .\myfile.txt | select-string -pattern "user:$user"
     write-host $count
}
#save the CSV

这有意义吗?我非常愿意听取您的提示和建议。 由于了解PowerShell的强大之处,我相信有一种更好的方法。谢谢!

2个回答

3

您当前的方法会为每个用户从磁盘读取一次文件。最好在单次扫描中收集所有用户,而不是多次扫描。

听起来您没有预先知道用户列表,基本上需要扫描类似于user:<username here>这样的字符串,并保持您发现的不同用户名的累计。

以下是应该完成基本工作的函数:

function GetUserCounts($fileName)
{
  $userCounts = @{}

  switch -regex -file $fileName
  {
    '\buser:([a-zA-Z]+)\b' {
       $userName = $matches[1]
       $userCounts[$userName] = [int]$userCounts[$userName] + 1
    }
  }

  $userCounts.GetEnumerator() | select Name,Value
}

那么您可以像这样创建一个CSV文件:
PS> GetUserCounts .\myfile.txt | Export-Csv .\counts.csv

0
这是另一种使用 Group-Object 命令的选项:
Get-Content lorem.txt | 
Foreach-Object {$_ -replace '^.+user:(.+)$','$1' } | 
Group-Object -NoElement

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接