Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

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.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Get-Content and Group-Object

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 01-01-2008   #1 (permalink)
Kam-Hung Soh
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 ...
Quote:

> "add", "dad", "dam", "mad", "made", "madam", "set" | group-object { $_.length }
Count Name Group
----- ---- -----
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 }
Count Name Group
----- ---- -----
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 }
Count Name Group
----- ---- -----
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 SpecsSystem Spec
Old 01-01-2008   #2 (permalink)
Jacques Barathon [MS]
Guest


 

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}
This is likely a bug in v1 since I cannot reproduce it with the CTP of v2.

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 SpecsSystem Spec
Old 01-01-2008   #3 (permalink)
Shay Levi
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


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 }
>>
> Count Name Group
>
> ----- ---- -----
> 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 SpecsSystem Spec
Old 01-01-2008   #4 (permalink)
Shay Levi
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


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 }
>>
> Count Name Group
>
> ----- ---- -----
> 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 SpecsSystem Spec
Old 01-01-2008   #5 (permalink)
Shay Levi
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


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 }
>>
> Count Name Group
>
> ----- ---- -----
> 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 SpecsSystem Spec
Old 01-01-2008   #6 (permalink)
Kam-Hung Soh
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:
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:
>
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: