2013-05-02 36 views
1

我有一個日誌文件,在那裏我試圖存儲所有行,一旦找到This has happened due to.,然後新行開始。在perl中搜索特定行後的行

所以基本上日誌看起來像這樣

Bla bla bla bla....This has happened due to. 

ABC 

DEF 

GHI 

JKL 

可以請你幫我到paatern匹配,一旦我找到了行「這已經發生了,由於」。

回答

4
while (<>) { 
    next unless /This has happened due to/; 
    while (<>) { 
    # Process lines 
    } 
    last; 
} 
+0

請將您的答案更改爲「文件輸入」 – gaussblurinc 2013-05-02 10:05:12

+1

@loldop我更喜歡編寫處理stdin的腳本,除非有某些原因不行。他總能適應他的特殊需求。 – Barmar 2013-05-02 10:14:49

+0

在我的測試中,這會導致解釋器永遠掛起。 – 2013-05-02 13:03:37

3
#!/usr/bin/env perl 

while (<>) { 
    last if /This has happened due to/; 
} 

while (<>) { 
    print; # do smth with the line 
} 

保存腳本文件並使其可執行。然後運行如./script.pl logfile