I'm looking for a way to filter a list of strings in powershell by a regex, then group and sort by one of the capture groups.
Imagine my list is like this:
bogus0
ACBXYZ-0000 hello
bogus1
ACBXYZ-0000 hello again
bogus2
ACBXYZ-0001 world
bogus3
ACBXYZ-0001 world
First i've done this:
$list | select-string "^(ACBXYZ-\d+)(.*)"
Which outputs
ACBXYZ-0000 hello
ACBXYZ-0000 hello again
ACBXYZ-0001 world
ACBXYZ-0001 world
Then i've done this:
$list | select-string "^(ACBXYZ-\d+)(.*)" | % { "$($_.Matches[0].Groups[1].Value), $($_.Matches[0].Groups[2].Value.Trim(' ,-'))" } | sort | group | select name
Which outputs
Name
----
ACBXYZ-0000, hello
ACBXYZ-0000, hello again
ACBXYZ-0001, world
But actually i would like to output this:
Name
----
ACBXYZ-0000, hello
ACBXYZ-0001, world
since the message after the number is nice to have but not really important.
Any ideas?
PS.: I was able to achieve that with a more complicated script, but i was looking for a one-liner.
Best Answer
In answering and researching for this question on SO, I believe I have a solution that's alligned to your original intent.
No doubt this can be shortened further (this code-golf should sort that out) but this would work for what you have posted
Try-it-online