![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | Get-Content and Group-Object Hi, I would like to read a text file and group words based on a property using group-object. For example, to group words by their length ... Quote: > "add", "dad", "dam", "mad", "made", "madam", "set" | group-object { $_.length } ----- ---- ----- 5 3 {add, dad, dam, mad...} 1 4 {made} 1 5 {madam} But when I try to process a file, the Group column doesn't show the input objects: Quote: > get-content test.txt | group-object { $_.length } ----- ---- ----- 5 3 {test.txt, test.txt, test.txt, test.txt...} 1 4 {test.txt} 1 5 {test.txt} If I use [System.IO.File]::ReadAllLines(), I get the expected result: Quote: > [System.IO.File]::ReadAllLines("test.txt") | group-object { $_.length } ----- ---- ----- 5 3 {add, dad, dam, mad...} 1 4 {made} 1 5 {madam} Why does Group-Object show only the file name in the Group column when it has input from Get-Content? -- Kam-Hung Soh <a href="http://kamhungsoh.com/blog">Software Salariman</ a> |
My System Specs![]() |
| | #2 (permalink) |
| | Re: Get-Content and Group-Object "Kam-Hung Soh" <kamhung.soh@xxxxxx> wrote in message news:be38d86c-bb4e-4dc7-b236-b89e521f17e3@xxxxxx Quote: > But when I try to process a file, the Group column doesn't show the > input objects: > Quote: >> get-content test.txt | group-object { $_.length } > Count Name Group > ----- ---- ----- > 5 3 {test.txt, test.txt, test.txt, > test.txt...} > 1 4 {test.txt} > 1 5 {test.txt} By the way, you don't need to write {$_.length}: "group-object length" is enough for PowerShell to understand that you want it to group objects by their length property. Jacques |
My System Specs![]() |
| | #3 (permalink) |
| | Re: Get-Content and Group-Object Works fine in v1 and 2 (same output): PS > "add", "dad", "dam", "mad", "made", "madam", "set" | group length Count Name Group ----- ---- ----- 5 3 {add, dad, dam, mad...} 1 4 {made} 1 5 {madam} It is syntactically valid (on both versions): PS > "add", "dad", "dam", "mad", "made", "madam", "set" | group {$_.length} Count Name Group ----- ---- ----- 5 3 {add, dad, dam, mad...} 1 4 {made} 1 5 {madam} It's like writing it this way: "add", "dad", "dam", "mad", "made", "madam", "set" | group -property {$_.length} # since the first positional parameter is -property: # see the third example on the help for group-object. The Group column shows the output objects contain the elements of each group, not the file names. You can omit it by specifing the -noElement parameter: PS > "add", "dad", "dam", "mad", "made", "madam", "set" | group length -noElement Count Name ----- ---- 5 3 1 4 1 5 I couldn't reproduce your file name in the group column. Post the file if you want one to check it. Here's how to do it on a file content, courtesy of "Windows PowerShell in Action" book by Bruce Payette $s = get-content <path> $s = [string]::join(" ", $s) $words = $s.split(" `t", [stringsplitoptions]::RemoveEmptyEntries) $words | group length ----- Shay Levi $cript Fanatic http://scriptolog.blogspot.com Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic Quote: > Hi, > > I would like to read a text file and group words based on a property > using group-object. For example, to group words by their length ... > Quote: >> "add", "dad", "dam", "mad", "made", "madam", "set" | group-object { >> $_.length } >> > > ----- ---- ----- > 5 3 {add, dad, dam, mad...} > 1 4 {made} > 1 5 {madam} Quote: >> But when I try to process a file, the Group column doesn't show the >> input objects: >> >> get-content test.txt | group-object { $_.length } >> >> Count Name Group >> > 5 3 {test.txt, test.txt, test.txt, > test.txt...} > 1 4 {test.txt} > 1 5 {test.txt} Quote: >> If I use [System.IO.File]::ReadAllLines(), I get the expected result: >> >> [System.IO.File]::ReadAllLines("test.txt") | group-object { $_.length >> } >> >> Count Name Group >> > 5 3 {add, dad, dam, mad...} > 1 4 {made} > 1 5 {madam} Quote: >> Why does Group-Object show only the file name in the Group column >> when it has input from Get-Content? >> >> -- >> Kam-Hung Soh <a href="http://kamhungsoh.com/blog">Software >> Salariman</ |
My System Specs![]() |
| | #4 (permalink) |
| | Re: Get-Content and Group-Object BEWARE: When you use get-content and pipe it to group-object you'll have the length of the LINES instead of words, get-content streams the file content line by line. ----- Shay Levi $cript Fanatic http://scriptolog.blogspot.com Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic Quote: > Hi, > > I would like to read a text file and group words based on a property > using group-object. For example, to group words by their length ... > Quote: >> "add", "dad", "dam", "mad", "made", "madam", "set" | group-object { >> $_.length } >> > > ----- ---- ----- > 5 3 {add, dad, dam, mad...} > 1 4 {made} > 1 5 {madam} Quote: >> But when I try to process a file, the Group column doesn't show the >> input objects: >> >> get-content test.txt | group-object { $_.length } >> >> Count Name Group >> > 5 3 {test.txt, test.txt, test.txt, > test.txt...} > 1 4 {test.txt} > 1 5 {test.txt} Quote: >> If I use [System.IO.File]::ReadAllLines(), I get the expected result: >> >> [System.IO.File]::ReadAllLines("test.txt") | group-object { $_.length >> } >> >> Count Name Group >> > 5 3 {add, dad, dam, mad...} > 1 4 {made} > 1 5 {madam} Quote: >> Why does Group-Object show only the file name in the Group column >> when it has input from Get-Content? >> >> -- >> Kam-Hung Soh <a href="http://kamhungsoh.com/blog">Software >> Salariman</ |
My System Specs![]() |
| | #5 (permalink) |
| | Re: Get-Content and Group-Object Check MoW's version: http://thepowershellguy.com/blogs/po...text-file.aspx ----- Shay Levi $cript Fanatic http://scriptolog.blogspot.com Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic Quote: > Hi, > > I would like to read a text file and group words based on a property > using group-object. For example, to group words by their length ... > Quote: >> "add", "dad", "dam", "mad", "made", "madam", "set" | group-object { >> $_.length } >> > > ----- ---- ----- > 5 3 {add, dad, dam, mad...} > 1 4 {made} > 1 5 {madam} Quote: >> But when I try to process a file, the Group column doesn't show the >> input objects: >> >> get-content test.txt | group-object { $_.length } >> >> Count Name Group >> > 5 3 {test.txt, test.txt, test.txt, > test.txt...} > 1 4 {test.txt} > 1 5 {test.txt} Quote: >> If I use [System.IO.File]::ReadAllLines(), I get the expected result: >> >> [System.IO.File]::ReadAllLines("test.txt") | group-object { $_.length >> } >> >> Count Name Group >> > 5 3 {add, dad, dam, mad...} > 1 4 {made} > 1 5 {madam} Quote: >> Why does Group-Object show only the file name in the Group column >> when it has input from Get-Content? >> >> -- >> Kam-Hung Soh <a href="http://kamhungsoh.com/blog">Software >> Salariman</ |
My System Specs![]() |
| | #6 (permalink) |
| | Re: Get-Content and Group-Object Hi, Just tested 2.0 CTP and the following statement works as expected: get-content C:\temp\download\doc\Language\test.txt | group-object length Count Name Group ----- ---- ----- 5 3 {add, dad, dam, mad...} 1 4 {made} 1 5 {madam} -- Kam-Hung Soh <a href="http://kamhungsoh.com/blog">Software Salariman</ a> On Jan 2, 7:43 am, Kam-Hung Soh <kamhung....@xxxxxx> wrote: Quote: > Hi, > > Thanks for your testing and suggestions. I'm using 1.0.0.0, so it > looks like I should check out 2.0 CTP. > > Best regards, > > -- > Kam-Hung Soh <a href="http://kamhungsoh.com/blog">Software Salariman</ > a> > > On Jan 2, 12:37 am, Shay Levi <n...@xxxxxx> wrote: > Quote: > > Check MoW's version:http://thepowershellguy.com/blogs/po...27/hey-powersh... Quote: > > ----- > > Shay Levi > > $cript Fanatichttp://scriptolog.blogspot.com > > Hebrew weblog:http://blogs.microsoft.co.il/blogs/scriptfanatic Quote: Quote: > > > Hi, Quote: Quote: > > > I would like to read a text file and group words based on a property > > > using group-object. For example, to group words by their length ... Quote: Quote: > > >> "add", "dad", "dam", "mad", "made", "madam", "set" | group-object { > > >> $_.length } Quote: Quote: > > > Count Name Group Quote: Quote: > > > ----- ---- ----- > > > 5 3 {add, dad, dam, mad...} > > > 1 4 {made} > > > 1 5 {madam} > > >> But when I try to process a file, the Group column doesn't show the > > >> input objects: Quote: Quote: > > >> get-content test.txt | group-object { $_.length } Quote: Quote: > > >> Count Name Group Quote: Quote: > > > ----- ---- ----- > > > 5 3 {test.txt, test.txt, test.txt, > > > test.txt...} > > > 1 4 {test.txt} > > > 1 5 {test.txt} > > >> If I use [System.IO.File]::ReadAllLines(), I get the expected result: Quote: Quote: > > >> [System.IO.File]::ReadAllLines("test.txt") | group-object { $_.length > > >> } Quote: Quote: > > >> Count Name Group Quote: Quote: > > > ----- ---- ----- > > > 5 3 {add, dad, dam, mad...} > > > 1 4 {made} > > > 1 5 {madam} > > >> Why does Group-Object show only the file name in the Group column > > >> when it has input from Get-Content? Quote: Quote: > > >> -- > > >> Kam-Hung Soh <a href="http://kamhungsoh.com/blog">Software > > >> Salariman</ |
My System Specs![]() |
| | #7 (permalink) |
| | Re: Get-Content and Group-Object "Kam-Hung Soh" <kamhung.soh@xxxxxx> wrote in message news:25506d0e-65d7-462c-abc7-6ec808eae965@xxxxxx Quote: > Hi, > > Just tested 2.0 CTP and the following statement works as expected: > > get-content C:\temp\download\doc\Language\test.txt | group-object > length > > Count Name Group > ----- ---- ----- > 5 3 {add, dad, dam, mad...} > 1 4 {made} > 1 5 {madam} > word *unless* there is only one word per line. Like Shay suggested, you probably want to do something like this *if* there are multiple words per line: [IO.File]::ReadAllText("C:\temp\download\doc\Language\test.txt").Split($null, [StringSplitOptions]::RemoveEmptyEntries) | group length BTW the String.Split method isn't very powerful and splitting on whitespace doesn't eliminate punctuation characters. You could use the static Split method on [regex] to get a better split: $contents = [IO.File]::ReadAllText("C:\temp\download\doc\Language\test.txt") [regex]::split($contents, '\s|[,;:.!?\(\)]') | where {$_} | group length You may need to tweak the regex pattern to split (ie remove) the appropriate punctuation. -- Keith |
My System Specs![]() |
| | #8 (permalink) |
| | Re: Get-Content and Group-Object On Jan 2, 1:26 pm, "Keith Hill [MVP]" <r_keith_h...@xxxxxx_spam_I> wrote: Quote: > "Kam-Hung Soh" <kamhung....@xxxxxx> wrote in message > > news:25506d0e-65d7-462c-abc7-6ec808eae965@xxxxxx > Quote: > > Hi, Quote: > > Just tested 2.0 CTP and the following statement works as expected: Quote: > > get-content C:\temp\download\doc\Language\test.txt | group-object > > length Quote: > > Count Name Group > > ----- ---- ----- > > 5 3 {add, dad, dam, mad...} > > 1 4 {made} > > 1 5 {madam} > It appears to me you are grouping on the length of each line and not each > word *unless* there is only one word per line. Like Shay suggested, you > probably want to do something like this *if* there are multiple words per > line: > > [IO.File]::ReadAllText("C:\temp\download\doc\Language\test.txt").Split($null, > [StringSplitOptions]::RemoveEmptyEntries) | group length > > BTW the String.Split method isn't very powerful and splitting on whitespace > doesn't eliminate punctuation characters. You could use the static Split > method on [regex] to get a better split: > > $contents = [IO.File]::ReadAllText("C:\temp\download\doc\Language\test.txt") > [regex]::split($contents, '\s|[,;:.!?\(\)]') | where {$_} | group length > > You may need to tweak the regex pattern to split (ie remove) the appropriate > punctuation. > > -- > Keith line in my files. Thanks to Shay and Keith for various methods to split a line into separate words. Regards, -- Kam-Hung Soh <a href="http://kamhungsoh.com/blog">Software Salariman</ a> |
My System Specs![]() |
| | #9 (permalink) |
| | Re: Get-Content and Group-Object "Shay Levi" <no@xxxxxx> wrote in message news:8766a944165608ca1a983d019fa6@xxxxxx Quote: > > Works fine in v1 and 2 (same output): > > PS > "add", "dad", "dam", "mad", "made", "madam", "set" | group length > > Count Name Group > ----- ---- ----- > 5 3 {add, dad, dam, mad...} > 1 4 {made} > 1 5 {madam} text file (one word per line as later specified by Kam-Hung) and you will see that the grouping sets do not show the lines. Instead they will repeatedly show the file name. Jacques |
My System Specs![]() |
| | #10 (permalink) |
| | Re: Get-Content and Group-Object Yes, I can see... try this: PS > cat words.txt add dad dam mad made madam set PS > cat words.txt | foreach {"$_"} | group length Count Name Group ----- ---- ----- 5 3 {add, dad, dam, mad...} 1 4 {made} 1 5 {madam} ----- Shay Levi $cript Fanatic http://scriptolog.blogspot.com Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic Quote: > "Shay Levi" <no@xxxxxx> wrote in message > news:8766a944165608ca1a983d019fa6@xxxxxx > Quote: >> Works fine in v1 and 2 (same output): >> >> PS > "add", "dad", "dam", "mad", "made", "madam", "set" | group >> length >> >> Count Name Group >> ----- ---- ----- >> 5 3 {add, dad, dam, mad...} >> 1 4 {made} >> 1 5 {madam} > a text file (one word per line as later specified by Kam-Hung) and you > will see that the grouping sets do not show the lines. Instead they > will repeatedly show the file name. > > Jacques > |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Group-Object and getting the full GroupInfo (Group) output. | PowerShell | |||
| Set-Content not updating file after get-content and forEach-Object | PowerShell | |||
| Group Policy Object Editor? | Vista account administration | |||
| Using group-object | PowerShell | |||
| Using group-object with a variable string | PowerShell | |||