2014-01-21 118 views
0

我想清除一些從命令中獲得的數據。 問題是數據針對不同情況重複,並在多行上傳播。 我在問題結尾添加了數據。正則表達式匹配某些行

我想用自己的狀態,以抓住Domain|Private|PublicON|OFF

一個例子輸出會是這樣

Domain OFF 
Private ON 
Public OFF 
因爲多條線路的

和我有限的正則表達式的知識,我只能匹配一行。 有人可以幫助我這個正則表達式。

數據樣本:

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. 
+0

你使用什麼語言?你有什麼嘗試? –

+0

你使用什麼應用程序?這看起來應該很簡單,使用'awk'。 – Barmar

+0

Powershell正則表達式-math命令。目前正在regexpal.com上測試[\ n \ r]。* State \ s *([^ \ n \ r] *)是我最近的代碼 –

回答

1

這解決了我的問題:

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

感謝所有幫助傢伙!

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

您將需要啓用dotallmultiline標誌 - 如何做到這一點取決於具體的環境。

以便首先識別並存儲範圍關鍵字,則跳過的隨機字符的非貪婪量直到State串是在一條線的開始遇到(由於^字符),然後任意的正則表達式在遇到ONOFF之前,允許空白字符的數量。作用域和狀態都作爲實際匹配返回。在中間使用.*?可以確保如果State不在第一行上,匹配也會起作用。

Sample here

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 

使用的-Context參數選擇字符串以獲取配置文件字符串後面的下兩行,並從PostContext解析設置。您可能需要使用-replace來替換拆分,具體取決於您的數據。

+0

獲取錯誤:即使寫主機顯示其內容,參數Path也是空的 –

+0

它適用於我,使用複製了測試數據的文件(例如$ file ='c:\ testfiles \ file1.txt') – mjolinor