![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
br> br> |
| |||||||
![]() |
| | Thread Tools | Display Modes |
| | #1 (permalink) | ||||||||||||||||||||||||||||||||||||
| Guest | 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 ...
----- ---- ----- 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:
----- ---- ----- 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:
----- ---- ----- 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) | ||||||||||||||||||||||||
| Guest | Re: Get-Content and Group-Object "Kam-Hung Soh" <kamhung.soh@xxxxxx> wrote in message news:be38d86c-bb4e-4dc7-b236-b89e521f17e3@xxxxxx
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) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Guest | 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
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
My System Specs![]() | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| | #4 (permalink) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Guest | 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
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
My System Specs![]() | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| | #5 (permalink) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Guest | 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
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
My System Specs![]() | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| | #6 (permalink) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Guest | 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:
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||