2016-09-23 33 views
0

首先,問一個非常簡單的問題,因爲我是全新的PowerShell的同學。如果搜索字符串匹配,只能讀取PowerShell中的特定字詞

我有文件,如:

"your>desired>output" START: this is th 
e output 
next line 
next line 
"your>desired>output" START: this is th 
e output 

如果「期望」這個詞被發現,不是隻讀「產出」比 「這是輸出」

如果我使用

我的任務是
Select-String *.txt -pattern "desired" 

大於輸出是

"your>desired>output" START: this is th 

「電子輸出」丟失。

任何形式的幫助表示讚賞。謝謝。

+1

'選擇字符串* .TXT 「期望」 -Context 1 |%{$ _線。 $ _。Context.PostContext}' –

+0

@ MathiasR.Jessen檢查一個關於上下文的欺騙,但你也可以做出答案(當然有一點解釋) – Matt

+0

@ MathiasR.Jessen謝謝你的迴應,但這並沒有返回任何東西...... :(顯示爲錯誤,我在「期望」之前使用了額外的模式 – user6633897

回答

2

如果您希望x行匹配前後,可以使用-Context參數。

簡要地標明你之前和比賽結束後要多少行,像這樣:

Select-String $path $pattern -Context 1 

這將使我們前1線和每場比賽後,1號線。您可以通過所產生的MatchInfo對象的Context屬性來訪問這些行:

Select-String *.txt "desired" -Context 1 |ForEach-Object { 
    # Output the matched line 
    $_.Line 
    # And then the next line 
    $_.Context.PostContext 
} 
+0

再次感謝,但它沒有給出正確的結果...... :( – user6633897