各ドキュメントでの単語の出現をカウントするように設計された次の機能を実行している一連のドキュメントがあります。この関数はコンソールへの出力で正常に機能しますが、情報を含むテキストファイルを生成したいのですが、リスト内の各単語にファイル名が追加されています。
私の現在のコンソール出力は次のとおりです。
"processing document1 with x unique words occuring as follows"
"word1 12"
"word2 8"
"word3 3"
"word4 4"
"word5 1"
この形式の区切りファイルが必要です。
document1;word1;12
document1;word2;8
document1;word3;3
document1;word4;4
document1;word1;1
document2;word1;16
document2;word2;11
document2;word3;9
document2;word4;9
document2;word1;13
以下の関数は単語と出現箇所のリストを取得しますが、各行の先頭に印刷されるようにfilename変数をどこにまたはどのように挿入するかを理解するのに苦労しています。MSDNはあまり役に立ちませんでした。変数を挿入しようとすると、ほとんどの場所でエラーが発生します(以下を参照)。
function Count-Words ($docs) {
$document = get-content $docs
$document = [string]::join(" ", $document)
$words = $document.split(" `t",[stringsplitoptions]::RemoveEmptyEntries)
$uniq = $words | sort -uniq
$words | % {$wordhash=@{}} {$wordhash[$_] += 1}
Write-Host $docs "contains" $wordhash.psbase.keys.count "unique words distributed as follows."
$frequency = $wordhash.psbase.keys | sort {$wordhash[$_]}
-1..-25 | %{ $frequency[$_]+" "+$wordhash[$frequency[$_]]} | Out-File c:\out-file-test.txt -append
$grouped = $words | group | sort count
出力ファイルコマンドレットに渡す文字列を作成する必要がありますか?これは私が最後の数回の試みで間違った場所に置いてきたものですか?なぜそれが特定の場所で起こっているのかを理解したいと思います。out-file
選択した結果を達成するために どこに置くべきかわからないので、今は推測しているだけです。
-$docs
とを使用して、PowerShellヘルプごとにコマンドをフォーマットしようとしまし-FilePath
たが、上記に正常に実行されるものを追加するたびにout-file
、次のエラーが発生します。
Out-File : Cannot validate argument on parameter 'Encoding'. The argument "c:\out-file-test.txt" does not bel
ong to the set "unicode,utf7,utf8,utf32,ascii,bigendianunicode,default,oem" specified by the ValidateSet attribute. Sup
ply an argument that is in the set and then try the command again.
At C:\c.ps1:39 char:71
+ -1..-25 | %{ $frequency[$_]+" "+$wordhash[$frequency[$_]]} | Out-File <<<< -$docs -width 1024 c:\users\x46332\co
unt-test.txt -append
+ CategoryInfo : InvalidData: (:) [Out-File], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.OutFileCommand