我有一系列文檔正在通過以下函數來設計用於統計每個文檔中的詞出現次數。這個函數可以很好地輸出到控制檯,但是現在我想生成一個包含信息的文本文件,但是文件名會附加到列表中的每個單詞上。我需要使用PowerShell的Out-File cmdlet幫助格式化輸出
我現在的控制檯輸出爲:
"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
雖然下面的功能得到了我的話,並出現次數的名單,我有很難找出在哪裏或如何插入文件名變量,以便它打印在每行的開頭。 MSDN已經不足,樂於助人,大部分的地方我嘗試插入變量導致錯誤(見下文)
function Count-Words ($docs) {
$document = get-content $docs
$document = [string]::join(" ", $document)
$words = $document.split(" `t",[stringsplitoptions]::RemoveEmptyEntries)
$uniq = $words | sort -uniq
$words | % {[email protected]{}} {$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
我需要創建一個字符串傳遞給了文件cmdlet的?這只是我在最後幾次嘗試中把錯誤的地方放在了什麼地方?我想了解爲什麼它會在特定的地方進行。現在我只是猜測,因爲我知道我不知道在哪裏可以讓out-file
達到我選擇的結果。
我試過每PowerShell幫助格式化我的命令,用-$docs
和-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
難道你不能編譯一個'PSObjec的數組列出所有內容,然後使用「Export-CSV -delimiter」;「 -notypeinfo'獲取輸出?從I/O角度來看,這會更有效率(繞過管道),只寫出一次文件。 – alroc 2013-02-15 14:18:27
@Graimer謝謝!這看起來不錯。這可能是我的環境的一個問題,但是當我如上所述運行代碼時,出現此錯誤......由於[System.Object []]不包含名爲'Split'的方法,所以方法調用失敗。 '在C:\ users \ x46332 \ desktop \ cgc \ counts.ps1:15 char:34'' +(Get-Content $ file).Split <<<<(「」,''[System.StringSplitOptions] :: RemoveEmptyEntries)| Group-Object | %{' '+ CategoryInfo:InvalidOperation:(Split:String)[],RuntimeException' '+ FullyQualifiedErrorId:MethodNotFound'任何想法?是否有'include'訪問類庫? – dwwilson66 2013-02-15 14:32:00
...或者也許'(Get-Content $文件)。-split(「」,[System.StringSplitOptions] :: RemoveEmptyEntries)|組對象' – dwwilson66 2013-02-15 14:39:17