如果某一列与 Powershell 中的 CSV 部分或完全匹配,则返回一行

如果某一列与 Powershell 中的 CSV 部分或完全匹配,则返回一行

基本上,我想搜索一个字符串并以 CSV 格式返回任何完整或部分匹配。

假设我有一个$Shoes如下对象:

"Brand","Style","Color","Price"
"Reebok","Basketball","Red/Black","$50"
"Vans","Slip-On","White/Black","$60"
"Vans","Old Skool","Red/White","$60"
"Nike","Sneakers","Red","$50"
"Adidas","Sneakers","Blue","$55"

为了使其更具可读性,格式化为表格如下:

Brand       Style       Color       Price
-----       -----       -----       -----
Reebok      Basketball  Red/Black   $50
Vans        Slip-On     White/Black $60
Vans        Old Skool   Red/White   $60
Nike        Sneakers    Red         $50
Adidas      Sneakers    Blue        $55

输入后$Shoes -like '*Red*'返回:

@{Brand=Reebok; Style=Basketball; Color=Red/Black; Price=$50}
@{Brand=Vans; Style=Old Skool; Color=Red/White; Price=$60}
@{Brand=Nike; Style=Sneakers; Color=Red; Price=$50}

但我想要的输出是:

Brand       Style       Color       Price
-----       -----       -----       -----
Reebok      Basketball  Red/Black   $50
Vans        Old Skool   Red/White   $60
Nike        Sneakers    Red         $50

我在这里做错了什么?

答案1

我猜当我尝试时格式不正确 -match,结果找到了一个简单的解决方案......

这很简单$Shoes -match 'Red'

相关内容