我正在使用Powershell,但在使用Where-Object cmdlet时遇到了问题。I当前为select *,然后希望仅在字段等于Alabama时输出。此字段可以在任何列下,而不只是一列。
这就是我所拥有的:
select * | where {$_.state_name -eq 'Alabama'} . 这对state_name有效,但如果不单独处理,我无法获得所有列。我尝试过where{$_ -eq....},但这不起作用。
发布于 2015-04-15 00:54:08
有点像黑客,但是:
select * | where {($_ | ConvertTo-Csv -NoTypeInformation)[1] -like '*"Alabama"*'}发布于 2015-04-15 01:04:55
你必须遍历所有对象的属性并检查它是否包含单词'Alabama‘。
示例:
# Import CSV file and feed it to the pipeline
Import-Csv -Path .\My.csv |
# For each object
ForEach-Object {
# Psobject.Properties returns all object properties
# (Psobject.Properties).Value returns only properties' values
# -contains operator checks if array of values contains exact string 'Alabama'
# You can also use -like operator with wildcards, i.e. -like '*labama'
if(($_.PSObject.Properties).Value -contains 'Alabama')
{
# If any of the object properties contain word 'Alabama',
# write it to the pipeline, else do nothing.
$_
}
}https://stackoverflow.com/questions/29632665
复制相似问题