2013-06-24 24 views
0

我目前正在嘗試創建一個自動化進程來動態解析特別大的日誌文件(25MB +)的選擇,並通過Java Servlet將其返回給用戶。從日誌文件中獲取範圍模式中的特定行

由於這些日誌的大小,我試圖執行Linux解析命令來檢索與用戶相關的部分,然後將它們加載到內存中。這些部分可以分佈在整個日誌中。

我仍然處於獲得正則表達式和文本解析工具(比如sed)的早期階段,我希望有人能指出我正確的方向來解決當前的問題。

我有一系列的日誌是指一行上的特定項目(例如KEY1),後面是關於此項目的未知數量的信息行。

然後,日誌會切換到下一個項目,重複

我試圖找出是否有基於Linux文本命令的任意組合,這可能採取格式的文件

This is the first line and should not display 
This is a section containing the text KEY1 
Line 1 
Line 2 
Line 3 
Line 4 
This is a section containing the text KEY2 
BadLine 1 
BadLine 2 
This is a second section containing the text KEY1 
Line 5 
Line 6 
This is a section containing the text KEY3 
BadLine 3 
BadLine 4 
BadLine 5 
BadLine 6 
This is a third section containing the text KEY1 
Line 7 
Line 8 
Line 9 
This is the last line 

和返回:

This is a section containing the text KEY1 
Line 1 
Line 2 
Line 3 
Line 4 
This is a second section containing the text KEY1 
Line 5 
Line 6 
This is a third section containing the text KEY1 
Line 7 
Line 8 
Line 9 
This is the last line 

命令

sed -n '/KEY1/,/KEY2/p' file 

抓住第一部分的工作,但我很難找到一種通用的方式來提取我需要的一切。

任何幫助,將不勝感激。

感謝

- 編輯 -

2013/06/20 03:10:01 PM| FINE |S9180 |[Device] [ID:128] 
foo 
bar 
foo 
bar 
------------------------------------------ 
foo 
bar 
------------------------------------------ 
2013/06/20 03:10:02 PM| FINE |S9180 |[Device] [ID:132] 
Other foo 
Other bar 
------------------------------------------ 
Other foo 
Other bar 
Other foo 
Other bar 
------------------------------------------ 
2013/06/20 03:10:03 PM| FINE |S9180 |[Device] [ID:128] 
foo 
bar 
------------------------------------------ 
foo 
bar 
foo 
bar 
------------------------------------------ 
foo 
bar 

爲了澄清,這是我的工作的格式。我正在嘗試獲取日誌中特定設備的所有信息。例如下的鍵[ID:128]的所有文本,但下忽略所述部分[ID:132](或任何其他ID比ID:128因爲沒有該設備將在下面被未來特定順序)

+0

嘿,你能給一些合理的例子說明KEY1,KEY2和KEY3是怎麼樣的? –

+0

我已更新該帖子以供澄清。乾杯。 – JamesA

回答

2

代碼GNU ,一些編輯後:

sed -rn '/\[ID:[0-9]+\]/{/\[ID:128\]/!{s/.*\B(\[ID:[0-9]+\])\B.*/\1/;H}};${x;s/\n//;s/\]\n\[/\\]|\\[/g;[email protected](.*)]@/\\[ID:128\\]/,/\\\1\\]/\{/\\\1\\]/!p\}@p}' file|sed -nrf - file 

 
$cat file 
2013/06/20 03:10:01 PM| FINE |S9180 |[Device] [ID:128] 
foo 
bar 
foo 
bar 
------------------------------------------ 
foo 
bar 
------------------------------------------ 
2013/06/20 03:10:02 PM| FINE |S9180 |[Device] [ID:132] 
Other foo 
Other bar 
------------------------------------------ 
Other foo 
Other bar 
Other foo 
Other bar 
------------------------------------------ 
2013/06/20 03:10:03 PM| FINE |S9180 |[Device] [ID:128] 
foo 
bar 
------------------------------------------ 
foo 
bar 
foo 
bar 
------------------------------------------ 
foo 
bar 
2013/06/20 03:10:02 PM| FINE |S9180 |[Device] [ID:32] 
Other foo 
Other bar 
------------------------------------------ 
Other foo 
Other bar 
Other foo 
Other bar 
------------------------------------------ 
2013/06/20 03:10:03 PM| FINE |S9180 |[Device] [ID:128] 
foo 
bar 
------------------------------------------ 
foo 
bar 
foo 
bar 
------------------------------------------ 
foo 
bar 
2013/06/20 03:10:02 PM| FINE |S9180 |[Device] [ID:132] 
Other foo 
Other bar 
------------------------------------------ 
Other foo 
Other bar 
Other foo 
Other bar 
------------------------------------------ 
2013/06/20 03:10:03 PM| FINE |S9180 |[Device] [ID:17] 
foo 
bar 
------------------------------------------ 
foo 
bar 
foo 
bar 
------------------------------------------ 
foo 
bar 

$sed -rn "/\[ID:[0-9]+\]/{/\[ID:128\]/!{s/.*\B(\[ID:[0-9]+\])\B.*/\1/;H}};${x;s/\n//;s/\]\n\[/\\]|\\[/g;[email protected](.*)]@/\\[ID:128\\]/,/\\\1\\]/\{/\\\1\\]/!p\}@p}" file|sed -nrf - file 
2013/06/20 03:10:01 PM| FINE |S9180 |[Device] [ID:128] 
foo 
bar 
foo 
bar 
------------------------------------------ 
foo 
bar 
------------------------------------------ 
2013/06/20 03:10:03 PM| FINE |S9180 |[Device] [ID:128] 
foo 
bar 
------------------------------------------ 
foo 
bar 
foo 
bar 
------------------------------------------ 
foo 
bar 
2013/06/20 03:10:03 PM| FINE |S9180 |[Device] [ID:128] 
foo 
bar 
------------------------------------------ 
foo 
bar 
foo 
bar 
------------------------------------------ 
foo 
bar 

sed[ID:128]之外的所有密鑰均以「Regex」模式/\[ID:[0-9]+\]/「收集」。第二次調用使用收集的密鑰過濾不需要的部分。

+0

謝謝,這當然有幫助,但我需要通過一個25MB +日誌文件來做到這一點,其中密鑰可以以隨機順序進入。我需要更通用的東西來抓出大塊。例如在** [ID:128] **和** [ID:SomeOtherNumberThan128] **之間。這可能使用sed? (或任何其他的Linux文本解析工具)? – JamesA

+0

@JamesA進行了編輯,您可以測試新的'sed'代碼。 – captcha

+0

這個技巧。完善。乾杯。 – JamesA

0

我覺得更普遍的做法是:

perl -ne 'print if /KEY1/../KEY(?!1)/' input.txt | perl -ne 'print unless /KEY(?!1)/' 

perl -ne 'print if /ID:128/../ID:(?!128)/' file.txt | perl -ne 'print unless /ID:(?!128)/' 

這裏有一些重要的概念:(?!1)

  • 關鍵手段「KEY之後沒有1「
  • 」perl -ne「表示」默認禁用打印「
  • 因此,打印僅在文本與「KEY1行,任意行數,KEY行跟隨1」格式匹配時啓用
  • 第二次perl調用刪除KEY2和KEY3行否則打印

我想有一個更好的方式來刪除KEY2和KEY3行,但我無法弄清楚如何做到這一點:一些Perl主人可以幫助你更多!

+0

欣賞它,但我無法訪問Web服務器上的perl我將安裝它,所以我希望使用awk或sed的解決方案。 – JamesA

相關問題