Powershell Select-Object vs ForEach on Select-String 结果 [英] Powershell Select-Object vs ForEach on Select-String results

查看:60
本文介绍了Powershell Select-Object vs ForEach on Select-String 结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在变量 $mat 中调用 Select-String 的结果,从文件内容解析正则表达式:

Let's say I have the results of a call to Select-String in a variable $mat, parsing a regular expression from file contents:

$mat = cat errors.txt | Select-String "'(?<code>\w+)'.+ID (?<id>[^:]+)" 

根据$mat |的输出Get-Member 结果包含 Match[] 类型的 Matches 属性.

According to the output of $mat | Get-Member the result contains a Matches property of type Match[].

当我执行以下命令时,我得到了正则表达式输出的所有匹配项:

When I execute the following I get all the matches of my regular expression output:

PS > $mat | Select-Object -Property Matches

Matches                                                                                                                                                                              
-------                                                                                                                                                                              
{'ACCFWD', ID 16}                                                                                                                                                                    
{'EQASIAN', ID 448}                   

为什么使用 foreach 选择匹配项的下一段代码没有相同的输出:

Why doesn't this next block of code using foreach to select the Matches have the same output:

    PS > $mat | ForEach { $_.Matches } 


Groups   : {'ACCFWD', ID 16, ACCFWD, 16}
Success  : True
Captures : {'ACCFWD', ID 16}
Index    : 20
Length   : 15
Value    : 'ACCFWD', ID 16

Groups   : {'EQASIAN', ID 448, EQASIAN, 448}
Success  : True
Captures : {'EQASIAN', ID 448}
Index    : 20
Length   : 17
Value    : 'EQASIAN', ID 448

谢谢!

推荐答案

当显示属性时,PowerShell 会自动格式化没有在 *.format.ps1xml 文件作为最多 4 个属性的表格.5 个或更多属性显示为列表.当您使用 Select-Object 选择 Matches 属性时,您选择的是 Microsoft.PowerShell.Commands.MatchInfo 对象的单个属性.使用 Foreach-Object,您可以显示 System.Text.RegularExpressions.Match 对象的所有属性.

When displaying properties, PowerShell auto-formats the properties of types that do not have a display format defined in a *.format.ps1xml file as a table for up to 4 properties. 5 or more properties displays as a list. When you select the Matches property with Select-Object, you are selecting a single property of the Microsoft.PowerShell.Commands.MatchInfo object. With Foreach-Object you are displaying all of the properties for a System.Text.RegularExpressions.Match object.

使用 Select-Object -ExpandProperty Matches 将导致输出看起来与 Foreach 相同,因为它将输出 RegularExpressions.Match 对象.

Using Select-Object -ExpandProperty Matches will cause the output to look the same as the Foreach because it will output RegularExpressions.Match objects.

如果将 Get-Member 放在生成输出的两个示例之后,您将看到它们输出不同类型的对象.

If you place Get-Member after both of your examples that produce output you will see that they output different types of objects.

这里是对每个命令发生的格式的解释.

Here is an explanation of the formatting that happens for each command.

cat errors.txt | Select-String "'(?<code>\w+)'.+ID (?<id>[^:]+)"

Select-String 的输出是一个 Microsoft.PowerShell.Commands.MatchInfo 对象,它有 8 个属性.默认情况下不显示这些属性,因为 MatchInfo 的显示格式在 PowerShellCore.format.ps1xml 中定义,以显示 MatchInfo 的结果ToString() 方法.

The output of Select-String is a Microsoft.PowerShell.Commands.MatchInfo object which has 8 properties. These properties are not displayed by default because the display format for MatchInfo is defined in PowerShellCore.format.ps1xml to show the result of MatchInfo's ToString() method.

$mat | Select-Object -Property Matches

在这种情况下,Select-Object 的输出是带有 Matches 的自定义 Selected.Microsoft.PowerShell.Commands.MatchInfo 对象从 MatchInfo 对象复制的属性.由于没有为 Selected.Microsoft.PowerShell.Commands.MatchInfo 类型定义默认显示格式,PowerShell 自动将其格式化为表格,因为它的属性少于 5 个(在这种情况下 Matches 是唯一的属性).

In this case, the output of Select-Object is a custom Selected.Microsoft.PowerShell.Commands.MatchInfo object with the Matches property that was copied from the MatchInfo object. Since there is no default display format defined for the Selected.Microsoft.PowerShell.Commands.MatchInfo type, PowerShell auto formats it as a table since it has less than 5 properties (In this case Matches is the only property).

$mat | ForEach { $_.Matches } 

Foreach-Object ScriptBlock 中,输出MatchInfo 对象的Matches 属性.Matches 属性是一个 System.Text.RegularExpressions.Match,它有 6 个属性.由于没有为 System.Text.RegularExpressions.Match 类型定义默认显示格式,Match 对象显示为列表,因为有超过 4 个属性.

In the Foreach-Object ScriptBlock, the Matches property of the MatchInfo object is being output. The Matches property is a System.Text.RegularExpressions.Match which has 6 properties. Since there is no default display format defined for the System.Text.RegularExpressions.Match type, the Match objects are displayed as a list because there are more than 4 properties.

这篇关于Powershell Select-Object vs ForEach on Select-String 结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆