我希望将管道中的结果连接起来。
我尝试使用
-join
。PS> type .\bleh.log | where { $_ -match "foo"} | select -uniq | $_ -join ','
但是这给我带来了错误:/
表达式只允许作为管道的第一个元素。
-join
。PS> type .\bleh.log | where { $_ -match "foo"} | select -uniq | $_ -join ','
但是这给我带来了错误:/
表达式只允许作为管道的第一个元素。
你可以尝试这个:
@(type .\bleh.log | where { $_ -match "foo"} | select -uniq) -join ","
在最后一个管道符之后,您需要使用Foreach-Object
(别名为%
),才能使$_
变量可用,但它并没有帮助,因为它仅包含单个单元格值(对于每个循环迭代)。
这段代码搜索文件中的字符“ya”,并将其作为数组返回一个唯一列表。然后,它将数组元素连接成一个由逗号分隔的字符串。
@(type requirements.txt | where {($_).Trim() -match "ya"} | select -Unique) -join ","
Join-String
命令。Get-Content .\bleh.log |
Where-Object { $_ -match 'foo' } |
Select-Object -Unique |
Join-String -Separator ','
function pipejoin {
@($input) -join $args[0]
}
0..10 | pipejoin ','
Export-Csv
。 - sodawillow