I wish to join the result from a pipe.
I tried using -join
PS> type .\bleh.log | where { $_ -match "foo"} | select -uniq | $_ -join ','
But that give me this error :/
Expressions are only allowed as the first element of a pipeline.
I wish to join the result from a pipe.
I tried using -join
PS> type .\bleh.log | where { $_ -match "foo"} | select -uniq | $_ -join ','
But that give me this error :/
Expressions are only allowed as the first element of a pipeline.
You could try this :
@(type .\bleh.log | where { $_ -match "foo"} | select -uniq) -join ","
You would need a Foreach-Object
(alias %
) after the last pipe to have the $_
variable available but it wouldn't help since it holds a single cell value (for each loop iteration).
Export-Csv
. –
Washwoman this code search the file for the characters "ya" and returns a unique list as an array. It then takes the array elements and joins them into a string separated by a comma
@(type requirements.txt | where {($_).Trim() -match "ya"} | select -Unique) -join ","
As an alternative PowerShell 6.2 and above comes with the Join-String
cmdlet:
Get-Content .\bleh.log |
Where-Object { $_ -match 'foo' } |
Select-Object -Unique |
Join-String -Separator ','
A simple but not very reliably non-advanced function for Windows PowerShell could be:
function pipejoin {
@($input) -join $args[0]
}
0..10 | pipejoin ','
© 2022 - 2024 — McMap. All rights reserved.