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 に答える