分析文件并使用Powershell计算单词

时间:2017-12-11 21:34:53

标签: powershell scripting

我正在尝试创建一个脚本,该脚本将输入一个文件,然后让它仅输出出现5次或更多次的单词。这就是我想出来的,但它只会将阵列分成线条...想法?

clear-host 
write-host 

$file = 'file.txt' 

foreach ($word in (get-content $file))
{    
  $a = $word.split(" .,?()") 

  foreach ($occ in $a -ge 5)
  {
    ? -ge 5 (Write-Host $occ )
  }
}  

1 个答案:

答案 0 :(得分:0)

$file = "file.txt"

$contents = Get-Content -Path $file

$words = $contents.split(" .,?()") | Where-Object {$_} # this removes any empties/spaces

Write-Host "Grouped:" -ForegroundColor Cyan
$words | Group-Object

Write-Host "Filtered:" -ForegroundColor Cyan
$words | Group-Object | Where-Object Count -GE 5