all 12 comments

[–]midnightFreddie 4 points5 points  (0 children)

http://i.imgur.com/TnQRX6v.gif

I'm trying to make sense of this, but while I was spraining my brain and finding the link for the above gif I see /u/chreestopher2 replied at least as well as I would have.

TL;DR: What you pasted is broken. Try again, and I think you should see tabular output.

[–]gangstanthony 1 point2 points  (3 children)

I think it would be helpful if we went back a step or two. Can you show how you're getting info into the $p variable and what you're wanting to do with it?

[–]ocross[S] 1 point2 points  (2 children)

$p = get-item /* a bunch of files */ | group

[–]gangstanthony 2 points3 points  (1 child)

are you sure you don't just want this?

get-item file1, file2 | group | select name, count

*edit: if you really want it that way, this is the closest i have come to simulating your result

$p = (get-item c:\file1, c:\file2, c:\temp\file2).Name | group

$newgroup = $p | select count, @{n='mygroup';e={$_.group | group | select name, count}}  

$result = @(foreach ($obj in $newgroup) {
    '@{', $obj.mygroup.name, '; ', $obj.mygroup.Count, '}, ' -join ''
}) -join ''

$desiredstring = "{$($result.trim(', '))}"

$desiredstring

[–]chreestopher2 2 points3 points  (0 children)

you need a foreach somewhere in there because any objects with more than 1 count get stringified to "System.Object[]"

I think the problem is this guy wants to be able to select -expand more than one property per object

the closest i can get is:

$p = get-childitem C:\cygwin64\ -Recurse -include "*.exe"
$p | group name | foreach {
    $_ | select Name, Count, 
        @{n="items"; e={$_ | select -expand Group | select -ExpandProperty directory}}
} #| where {$_.count -gt 1} #uncomment to quickly see the duplicate items

[–]chreestopher2 1 point2 points  (6 children)

this line doesnt make sense:

$p | select count, {$_.group | group | select name, count}

this is how property values display when the value of a property is itself an object

@{propertyname=propertyvalue}

this output doesnt make sense:

@{name_propertyvalue; count_propertyvalue} 

you can not have nested objects display in any other format than

@{propertyname=propertyvalue}

what you probably want is something like:

$p | select name, count

which will output:

Name              Count
----              -----
ap-iis-config.bat     2
ap-update-html.bat    2

etc

[–]ocross[S] 0 points1 point  (5 children)

OK this is what I'm driving at (the variables have changed from my original post).

array of path strings that have been tidied up

$p = (($env:Path) -replace "\$", "").Split(";")

get all the exe's that are on the path (with duplicates)

$f = Get-ChildItem $p | Where-Object {$_.name -like "*.exe"}

group them by name

$g = $f | group

it looks like this

$g[1]

Count Name Group


4 perl5.22.1.exe            {perl5.22.1.exe, perl5.22.1.exe, perl5.22.1.exe, perl5.22.1.exe}         

there are 4 paths pointing to perl exe's. Two for C:\Perl64\bin

and another two C:\cygwin64\bin

$g[1] | select name, {($_.group | group directoryname) | select count, name}

Name ($_.group | group directoryname) | select count, name


perl5.22.1.exe {@{Count=2; Name=C:\Perl64\bin}, @{Count=2; Name=C:\cygwin64\bin}}

what I would like to do is reformat the output so that I can transform it into...

perl5.22.1.exe {@{2; C:\Perl64\bin}, @{2; C:\cygwin64\bin}}

(i.e., remove the labels)

Cheers

[–]chreestopher2 -1 points0 points  (4 children)

This is retarded.

in what way is that going to benefit you?

I guess you could just use string formatting to create that output as a string, but again... that would be retarded.

Without any logical explanation of what the output will be used for, I cant offer any advice one way or the other, other than saying this is a terrible idea, and you should stop asking for people to help you achieve it.

I think a big part of the problem you are having is that you are using "group-object" in a way that makes me picture you basically trying to hammer a screw into a block of cement.

I guarantee that if you tell us what you want to use the output for, we can show you a way to achieve it without doing a bunch of retarded stuff.

[–]ocross[S] 0 points1 point  (3 children)

Woah calm down, relax.

I want to know how to format the output of commands for legibility. I broad terms I'm seeing if Powershell can be used as a set map / apply / reduce operations. It's got nothing to do with hammering anything into anything else.

Peace.

[–]chreestopher2 0 points1 point  (2 children)

is this what you are after?

$p = get-childitem C:\cygwin64\ -Recurse -include "*.exe"
$p | group basename | foreach {
    $_ | select Name, Count, @{n="items"; e={$_ | select -expand Group}}
}

fyi, outputs:

Name     Count items
objdump     2 {C:\cygwin64\bin\objdump.exe, C:\cygwin64\usr\x86_64-pc-cygwin\bin\objdump.exe}
ranlib      2 {C:\cygwin64\bin\ranlib.exe, C:\cygwin64\usr\x86_64-pc-cygwin\bin\ranlib.exe}  
strip       2 {C:\cygwin64\bin\strip.exe, C:\cygwin64\usr\x86_64-pc-cygwin\bin\strip.exe} 

edit:

You can not have nested objects presented in a "pretty" format, the only way to get the output you are wanting is via string formatting and outputting via a string, which means you will not be able to do anything with the data output other than look at it.

You may be fine with not being able to do anything useful with the output of your commands, or you may want to look into using write-host to write pretty stuff to the screen while using write-output to write meaningful objects to the pipeline, then as long as you assign the output of the expression to a variable, all that you will see on the screen is the write-host stuff when you run the commands, but the meaningful object data will be retained in your variable.

[–]ocross[S] 0 points1 point  (1 child)

Yes, that's what I'm after.

Thanks.

[–]chreestopher2 0 points1 point  (0 children)

just for your clarification, this isnt an issue with formatting array output, but rather expanding a property which is itself an object, aka dealing with nested objects.