2015-12-24 81 views
0

我想使用AWK在Windows應用程序事件日誌中搜索字符串。以下是摘錄日誌:如何在AWK的搜索結果中打印缺少的字段?

W 05-Nov-14 10:09:36 261 CA_OSC   <I>Process ax_be has finished without having received an explicit termination request from the component manager </I> 
                    Time: 5.11.2014, 10:09:36, Line: 1161, File: \MCom\src\OSS\compmgr\src/CaCompPCB.cpp, Process: CaGenericMain (2448) 
E 05-Nov-14 10:09:36 17 AY_ISC   An error was detected in a process that is monitored by State Manager.  
                    Time: 5.11.2014, 10:09:36, Process: C:\AXM\Service\bin\Rep.exe_1976, 
                    Text: (05.11.2014 10:09:36) IVS SET: CMonitorThread::ProcessTermination(AppBE,5452) A critical process has 
W 05-Nov-14 10:09:37 261 CA_OSC   <I>Process main_ui has finished without having received an explicit termination request from the component manager </I> 
                    Time: 5.11.2014, 10:09:37, Line: 1161, File: \MCom\src\OSS\compmgr\src/CaCompPCB.cpp, Process: CaGenericMain (2448) 

我要搜索的字符串說,在事件日誌「ProcessTermination」,檢索輸出應該如下:

E 05-Nov-14 10:09:36 17 AY_ISC   An error was detected in a process that is monitored by State Manager.  
                    Time: 5.11.2014, 10:09:36, Process: C:\AXM\Service\bin\Rep.exe_1976, 
                    Text: (05.11.2014 10:09:36) IVS SET: CMonitorThread::ProcessTermination(AppBE,5452) A critical process has 

即。日誌和所有總結行發現匹配時。 搜索字符串可以在日誌行或任何摘要行中。 每行由新行字符分隔,並且此日誌文件是一個.txt文件。

到目前爲止,我已經嘗試下面的命令:

awk -v RS="\n(E|I|W)" "/ProcessTermination/" XA135420_2014_11_05_AppEventLog.txt 

但結果,E | I | W缺失。即。在結果我(日誌行的第一個字段)|我有像

05-Nov-14 10:09:36 17 AY_ISC   An error was detected in a process that is monitored by State Manager.  
                    Time: 5.11.2014, 10:09:36, Process: C:\AXM\Service\bin\Rep.exe_1976, 
                    Text: (05.11.2014 10:09:36) IVS SET: CMonitorThread::ProcessTermination(AppBE,5452) A critical process has 

結果誰能幫我列出W |é?

注:我使用了GNU AWK 3.1.6在Windows 7

+0

我測試你的代碼sccuess。 Cygwin awk 4.1.3 – bian

+0

@ A-Ray:有什麼辦法可以通過GNU AWK實現這一點? – Smij01

+0

@ Smij01 cygwin awk是GNU AWK。這不是你目前問題的原因,但得到一個新的gawk - 你的方式是過時的,你錯過了很多非常有用的功能和錯誤修復。 –

回答

0

負載段落中的內存使用awk

awk '/^[EIW]/{if(P ~ /ProcessTermination/)print P;P=""}{P=P"\n"$0}END{if(P ~ /ProcessTermination/)print P}' XA135420_2014_11_05_AppEventLog.txt 

的字段定義,你可以使用FPAT而不是FS來定義一個字段內容,而不是字段分隔符,但我不知道「RPAT

+0

我們不能避免這麼長的命令嗎?我已經有一個類似於你的命令:awk'{if($ 1〜/ ^(E | I | W)$ /){if(print_match){print line; print_match = 0} line = $ 0} else {line = line ORS $ 0}}/ProcessTermination/{print_match = 1} END {if(print_match){print line;}}'XA135420_2014_11_05_AppEventLog.txt ....但是我想減少腳本的長度如何 – Smij01

0

通過設置RS像你一樣,你是從記錄中刪除匹配RS的字符串,並使您的腳本特定於gawk,其中沒有一個是desirir能夠。你可以通過保存每個RT值和下一個記錄之前打印出來解決這個問題:

$ awk -v RS='(^|\n)\\S' '/ProcessTermination/{print gensub(/^\n|\n$/,"","g",p$0)} {p=RT}' file 
E 05-Nov-14 10:09:36 17 AY_ISC   An error was detected in a process that is monitored by State Manager. 
                    Time: 5.11.2014, 10:09:36, Process: C:\AXM\Service\bin\Rep.exe_1976, 
                    Text: (05.11.2014 10:09:36) IVS SET: CMonitorThread::ProcessTermination(AppBE,5452) A critical process has 

gensub()必須消除換行符,每RT除了與第一啓動以來RS開始以新行或啓動-of文件,並刪除該文件中的最後一條記錄用,因爲它沒有後續的RS-比賽,大吃它在自然終止換行符結束其後的換行符。

的更清晰,更簡單的解決方案不使用RT並在任何awk中工作:

$ cat tst.awk 
/^[WEI]/ { check() } 
{ buf = buf $0 RS } 
END { check() } 
function check() { 
    if (index(buf,tgt)) { 
     printf "%s", buf 
    } 
    buf = "" 
} 
$ 
$ awk -v tgt="ProcessTermination" -f tst.awk file 
E 05-Nov-14 10:09:36 17 AY_ISC   An error was detected in a process that is monitored by State Manager. 
                    Time: 5.11.2014, 10:09:36, Process: C:\AXM\Service\bin\Rep.exe_1976, 
                    Text: (05.11.2014 10:09:36) IVS SET: CMonitorThread::ProcessTermination(AppBE,5452) A critical process has 

注意使用index()上面,而不是/.../match(),如果你真的不想要搜索的字符串,而不是一個正則表達式。