I need help formatting output with PowerShell's Out-File cmdlet -



I need help formatting output with PowerShell's Out-File cmdlet -

i have series of documents going through next function designed count word occurrences in each document. function works fine outputting console, want generate text file containting information, file name appended each word in list.

my current console output is:

"processing document1 x unique words occuring follows" "word1 12" "word2 8" "word3 3" "word4 4" "word5 1"

i want delimited file in format:

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

while function below gets me lists of words , occurences, i'm having hard time figuring out or how insert filename variable prints @ head of each line. msdn has been less-than helpful, , of places seek insert variable result in errors (see below)

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 follows." $frequency = $wordhash.psbase.keys | sort {$wordhash[$_]} -1..-25 | %{ $frequency[$_]+" "+$wordhash[$frequency[$_]]} | out-file c:\out-file-test.txt -append $grouped = $words | grouping | sort count

do need create string pass out-file cmdlet? i've been putting in wrong place on lastly few tries? i'd understand why it's going in particular place well. right i'm guessing, because know have no thought set out-file accomplish selected results.

i've tried formatting command per powershell help, using -$docs , -filepath, each time add together out-file above runs successfully, next error:

out-file : cannot validate argument on parameter 'encoding'. argument "c:\out-file-test.txt" not bel ong set "unicode,utf7,utf8,utf32,ascii,bigendianunicode,default,oem" specified validateset attribute. sup ply argument in set , seek command again. @ 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

i rewrote of code. should utilize objects create easier formatting way want. 1 splits on "space" , groups words together. seek this:

function count-words ($paths) { $output = @() foreach ($path in $paths) { $file = get-childitem $path ((get-content $file) -join " ").split(" ", [system.stringsplitoptions]::removeemptyentries) | group-object | select-object -property @{n="filename";e={$file.basename}}, name, count | % { $output += "$($_.filename);$($_.name);$($_.count)" } } $output | out-file test-out2.txt -append } $filepaths = ".\test.txt", ".\test2.txt" count-words -paths $filepaths

it outputs asked(document;word;count). if want documentname include extension, alter $file.basename $file.name . testoutput:

test;11;1 test;9;2 test;13;1 test2;word11;5 test2;word1;4 test2;12;1 test2;word2;2

powershell file-io

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -