正则表达式匹配某些行

时间:2014-01-21 16:31:47

标签: regex powershell

我正在尝试清理从命令中获取的一些数据。 问题是针对不同情况重复数据并在多行上传播。 我在问题的最后添加了数据。

我正在尝试使用州Domain|Private|Public

抓取ON|OFF

示例输出将是

Domain  OFF
Private ON
Public  OFF

由于多行和我对Regex的了解有限,我只能匹配一行。 有人可以帮我解决这个正则表达式。

数据样本:

Domain Profile Settings: 
----------------------------------------------------------------------
State                                 OFF
Firewall Policy                       BlockInbound,AllowOutbound
LocalFirewallRules                    N/A (GPO-store only)
LocalConSecRules                      N/A (GPO-store only)
InboundUserNotification               Enable
RemoteManagement                      Enable
UnicastResponseToMulticast            Enable

Logging:
LogAllowedConnections                 Disable
LogDroppedConnections                 Disable
FileName                              %systemroot%\system32\LogFiles\Firewall\pfirewall.log
MaxFileSize                           4096


Private Profile Settings: 
----------------------------------------------------------------------
State                                 OFF
Firewall Policy                       BlockInbound,AllowOutbound
LocalFirewallRules                    N/A (GPO-store only)
LocalConSecRules                      N/A (GPO-store only)
InboundUserNotification               Enable
RemoteManagement                      Enable
UnicastResponseToMulticast            Enable

Logging:
LogAllowedConnections                 Disable
LogDroppedConnections                 Disable
FileName                              %systemroot%\system32\LogFiles\Firewall\pfirewall.log
MaxFileSize                           4096


Public Profile Settings: 
----------------------------------------------------------------------
State                                 OFF
Firewall Policy                       BlockInbound,AllowOutbound
LocalFirewallRules                    N/A (GPO-store only)
LocalConSecRules                      N/A (GPO-store only)
InboundUserNotification               Enable
RemoteManagement                      Enable
UnicastResponseToMulticast            Enable

Logging:
LogAllowedConnections                 Disable
LogDroppedConnections                 Disable
FileName                              %systemroot%\system32\LogFiles\Firewall\pfirewall.log
MaxFileSize                           4096

Ok.

4 个答案:

答案 0 :(得分:3)

(Domain|Public|Private).*?^State\s+(ON|OFF)

您需要启用dotallmultiline标记 - 如何执行此操作取决于您的特定环境。

regexp按顺序首先识别并存储scope关键字,然后跳过非贪婪的随机字符,直到在行开头遇到State字符串(由于^字符),然后在遇到ONOFF之前允许任意数量的空格字符。范围和状态都作为实际匹配返回。如果.*?不在第一行,则在中间使用State可确保匹配也有效。

Sample here

答案 1 :(得分:3)

假设您正在使用符合PCRE的正则表达式,那么应该这样做:

/(Domain|Public|Private)\s+.*?State\s+(ON|OFF)/gs

演示:http://regex101.com/r/pG3iI6

答案 2 :(得分:1)

这解决了我的问题:

%{$_.Replace(' Profile Settings: ', '')} | 
            where {$_ -match '(Domain|Public|Private|State)'} |
            %{$_ -Replace('State *','')}

感谢所有帮助人员!

答案 3 :(得分:0)

试试这个:

$file = <filename to search>
select-string -Pattern 'Domain|Private|Public Profile Settings' -Path $file -Context 0,2 | 
foreach {[PSCustomObject]@{
                        Type = $_.line.split()[0]
                        Setting = $_.Context.PostContext[1].split()[-1]
                      }
    } | ft -AutoSize

使用Select-String的-Context参数来获取Profile字符串后面的下两行,并从PostContext解析该设置。您可能希望使用-replace替换拆分,具体取决于您的数据。