2010-10-20 160 views
2

我給出一個日誌文件(見下文),我需要使用bash腳本,使其向這種格式:shell腳本來分析日誌文件

 

         
 
title pdfspool date rip date bmpspool date CLAB date Sometitle12 10/09/23 00:56:40 10/9/23 0:56:46 10/9/23 0:56:50 10/9/23 1:01:13

日誌文件

 

!!Begin  
Source aserver:pdf_spool:the, Job 844b015e0043469e, Inst 844b015e0043469e  
Title Sometitle12.pdf  
Action Started Received, Ok Date 10/09/23 00:56:40  
For Administrator  
(8) DataType = PDF  
(17) Source = srv01:aserver:file_input:0  
!!End  
!!Begin  
Source aserver:rip:rip1, Job 844b015e0043469e, Inst 844b015e004346a0  
Title Sometitle12.pdf Cyan 1  
Action Started Transmit, Ok Date 10/09/23 00:56:46  
For Administrator  
(8) DataType = Bitmap  
(1) Destination = srv01:bserver:bmp_spool:the  
(4) Parent = 844b015e0043469e/844b015e0043469e  
!!End  
!!Begin  
Source bserver:bmp_spool:the, Job 844b015e0043469e, Inst 844b015e004346a0  
Title Sometitle12.pdf Cyan 1  
Action Started Received, Ok Date 10/09/23 00:56:50  
For Administrator  
(8) DataType = Bitmap  
(17) Source = srv01:aserver:rip:rip1  
!!End  
!!Begin  
Source bserver:bmp_spool:the, Job 844b015e0043469e, Inst 844b015e004346a0  
Title Sometitle12.pdf Cyan 1  
Action Atomic Accepted, Ok Date 10/09/23 01:01:13  
For Administrator  
(8) DataType = Bitmap  
(2) Source Queue = ^03Newspaper ltd(MP)^Date - 24MP^Site - N^  
(5) Requested By = clab  
(15) Approval Status = Waiting Approved  
Changed from Waiting to Approved by clab. 
!!End  

想法表示歡迎。

謝謝!

回答

2
awk 'BEGIN{} 
/Action Started Received/ && !c{ pdfspooldate=$(NF-1)$NF ;c++} 
/Action Started Received/ && c{ bmppooldate=$(NF-1)$NF ;c=0} 
/Action Started Transmit/{ ripdate=$(NF-1)$NF } 
/title/ { title=$2} 
/Action Atomic Accepted/{ clabdate=$(NF-1)$NF } 
END{ print title,pdfspooldate,ripdate,clabdate }' file 
+0

謝謝!它的效果很好,除了你跳過了與pdfspooldate相同模式的bmpspool日期。如何區分它們? – myschyk 2010-10-20 05:27:45

+0

你可以設置一個計數器。查看我的編輯 – ghostdog74 2010-10-20 05:35:25

+0

如何解析現在有多個日誌文件的文件? By,在櫃檯的方式我用$ c = 2,它的工作。 – myschyk 2010-10-22 07:36:32

2

使用awk。寫一個狀態機。當您看到/^!!Begin$/時切換狀態,記錄您的數據並轉儲您的輸出並在看到/^!!End$/時切換回來。

0

如果您使用Perl/Python/Ruby,您應該能夠在一行(匹配部分)中使用正則表達式匹配。使用.將匹配換行符的多行模式。我認爲AWK或sed中應該能夠使用正則表達式以同樣的方式:

例如,在Ruby中:

s = <<TEXT 
!!Begin 
Something haha 
Title Good Bad Ugly 
Date 1/1/2008 
!!End 
!!Begin 
Other info 
Title Iron Man 
Date 2/2/2010 
TEXT 

result = s.scan(/^!!Begin.*?^Title\s+([^\n]*).*?^Date\s+([^\n]*)/m) 

p result 

result.each do |arr| 
    puts arr.join(' ') 
end 

輸出:

$ ruby try.rb 
[["Good Bad Ugly", "1/1/2008"], ["Iron Man", "2/2/2010"]] 
Good Bad Ugly 1/1/2008 
Iron Man 2/2/2010 
+0

如何讀取文本文件並將其傳遞給「s」變量? get_file_as_text無效 – myschyk 2010-10-22 10:07:22

0

我會使用Perl與$/ = "!!End",然後解析每個段落。