1

我正在调整这个脚本来计算文件中单个单词的实例。

$txtPath = "c:\users\xxxxxx\desktop\tx"
$srcfiles = Get-ChildItem $txtPath -filter "*.txt*"
#
function wordCount ($docs) {
    Write-Host "Processing Word Count: " $docs
$s = "I saw the cat. The cat was black."
",",".","!","?",">","<","&","*","=","`n","_" |% {$s = $s.replace($_,' ')}     # Remove Chars
$w = $s.Split() |? {$_.Length -gt 0 }                                     # Array of words, spaces removed
$w | select -Unique                                                       # Unique words
$w | group                                                                # Tally
$W | group | sort name | ft name,count -AutoSize              # Sort and format
#>
}
#
ForEach ($doc in $srcfiles) { 
    Write-Host "Calling: " $doc.FullName         
    wordCount -docs  $doc.FullName    
}

$s目前,表示要计数的字符串的输入变量是硬编码的。我想获取$srcFiles路径中的每个文档并对每个文档进行计数。但是,$s = $docs计算标题中的单词,而不是文档内容。我怎么做?

此外,$W | group | sort name | ft name,count -AutoSize返回以下错误:

out-lineoutput : The object of type "Microsoft.PowerShell.Commands.Internal.Format.FormatStartData" is not valid or not
 in the correct sequence. This is likely caused by a user-specified "format-table" command which is conflicting with th
e default formatting.
    + CategoryInfo          : InvalidData: (:) [out-lineoutput], InvalidOperationException
    + FullyQualifiedErrorId : ConsoleLineOutputOutOfSequencePacket,Microsoft.PowerShell.Commands.OutLineOutputCommand

我应该在哪里寻找格式问题?我无法在 TechNet 上发现任何类型的默认格式信息;并且该代码最初来自的站点没有提及它们是如何使其工作的,也没有提及它们覆盖了哪些默认格式。我怀疑我可能需要以不同的方式对其进行管道传输,但我需要更好地理解确切的错误,以便我知道从哪里开始寻找。

4

1 回答 1

2

您需要使用Get-Content来读取文件的内容。您需要组合 Get-Content 返回的行以正确计算字符数。看到这个帖子

于 2013-01-11T17:28:21.037 回答