2013-10-03 371 views
-4

我正在嘗試逐行讀取一個特定字符串及其值的日誌文件。解析unix日誌文件

比如我有一個日誌文件,如下圖所示:

####<Sep 26, 2013 12:05:22 AM MDT> <Error> <comApp> <ap001> <Server12> <[ACTIVE]  ExecuteThread: '55' for queue: 'weblogic.kernel.Default (self-tuning)'> <<anonymous>> <> <> <1380175522589> <000000> <<ERROR>Exception ID: 971 - Rate with ResParameters-> MAT: SJC GHT: FJC PUD: Fri Sep 27 09:00:00 MDT 2013>  
####<Sep 26, 2013 12:05:22 AM MDT> <Error> <comApp> <ap001> <Server12> <[ACTIVE] ExecuteThread: '55' for queue: 'weblogic.kernel.Default (self-tuning)'> <<anonymous>> <> <> <1380175522593> <000000> <<ERROR>Exception ID: 971 - Rate with ResParameters-> MAT: SJC GHT: FJC PUD: Fri Sep 27 09:00:00 MDT 2013>  
####<Sep 26, 2013 12:05:22 AM MDT> <> <Error> <comApp> <ap001> <Server12> <[ACTIVE] ExecuteThread: '55' for queue: 'weblogic.kernel.Default (self-tuning)'> <<anonymous>> <> <> <1380175522597> <000000> <<ERROR>Exception ID: 971 - Rate with ResParameters-> MAT: SJC GHT: FJC PUD: Fri Sep 27 09:00:00 MDT 2013> 

我需要閱讀從定義字符串值MAT GHT:,:,PUD:

我試圖讓輸出:

SJC , FJC, Fri Sep 27 09:00:00 MDT 2013 
DJA , SJC, Fri Sep 27 09:00:00 MDT 2013 
FJC , KJC, Fri Sep 27 09:00:00 MDT 2013 
JJC , SJC, Fri Sep 27 09:00:00 MDT 2013 
+0

我覺得'sed'會成爲解決這個問題的好工具。你可以很容易地找到教程,並在這裏問問如果你有任何問題 –

回答

0
perl -F'\W*(?:MAT|GHT|PUD):\W*' -lane'[email protected]; s|>\s*$||[email protected]; print join", ",@F' file 

輸出

SJC, FJC, Fri Sep 27 09:00:00 MDT 2013 
SJC, FJC, Fri Sep 27 09:00:00 MDT 2013 
SJC, FJC, Fri Sep 27 09:00:00 MDT 2013 
1

使用sed

sed -r 's/.*MAT:\s*(\w+)\s+GHT:\s*(\w+)\s+PUD:\s*(.+)\s*>/\1, \2, \3/g' infile > outfile 

相同的搜索和替換可以在perl

perl -pe 's/.*MAT:\s*(\w+)\s+GHT:\s*(\w+)\s+PUD:\s*(.+)\s*>/\1, \2, \3/g' infile > outfile 

試驗中所使用的示例數據:

$ cat infile 
####<Sep 26, 2013 12:05:22 AM MDT> <<anonymous>> <> MAT: SJC GHT: FJC PUD: Fri Sep 27 09:00:00 MDT 2013 > 
####<Sep 26, 2013 12:05:22 AM MDT> <<anonymous>> <> MAT: DJA GHT: SJC PUD: Fri Sep 27 09:00:00 MDT 2013 > 
####<Sep 26, 2013 12:05:22 AM MDT> <<anonymous>> <> MAT: FJC GHT: KJC PUD: Fri Sep 27 09:00:00 MDT 2013 > 
####<Sep 26, 2013 12:05:22 AM MDT> <<anonymous>> <> MAT: JJC GHT: SJC PUD: Fri Sep 27 09:00:00 MDT 2013 > 

$ cat outfile 
SJC, FJC, Fri Sep 27 09:00:00 MDT 2013 
DJA, SJC, Fri Sep 27 09:00:00 MDT 2013 
FJC, KJC, Fri Sep 27 09:00:00 MDT 2013 
JJC, SJC, Fri Sep 27 09:00:00 MDT 2013 
+0

@ RitheshB.P似乎你的'sed'不支持擴展正則表達式。他們你需要逃避''/// g'中的字符。建議使用perl' – jkshah

+0

@ RitheshB.P嘗試在第二行使用'perl'解決方案 – jkshah

+0

讓我解釋一下:'-r'是'GNU sed'的開關,用於打開擴展正則表達式。由於你在Solaris上,並沒有使用'GNU sed',所以你沒有選擇。如果你閱讀'man sed',你可以找出哪個選項打開擴展正則表達式,它可能是'-E'。 –

1

既然你標記的Perl

perl -ne 'if($_=~/MAT: (\S+) GHT: (\S+) PUD: (\S+ \S+ \d+ \d\d:\d\d:\d\d \S+ \d\d\d\d)/){ print "$1,$2,$3\n" ;}' test.txt