2013-07-10 103 views
2

我需要搜索「WAM」在下面/var/log/messages和導出完整的時間戳值到Excel工作表/文本文檔shell腳本搜索和導出到CSV文件/ Excel表

2013-07-09T02:22:18.535639Z user.info WebAppMgr WAM 
2013-07-09T02:22:21.817372Z user.info sam SAM ^Icom.palm.app.calculator 
2013-07-09T02:22:21.818442Z user.info sam SAM ^Icom.palm.app.settings 
2013-07-09T02:22:22.746751Z user.info WebAppMgr WAM 
2013-07-09T02:22:23.846636Z user.info sam SAM ^Icom.palm.app.notes 
2013-07-09T02:22:24.851727Z user.info sam SAM ^Icom.palm.app.firstuse 

對於終端輸出,這將工作

awk '/\ WAM/ {print $1"\t"}' /home/santosh/messages 

我需要在文本文件/ Excel表單輸出像

WAM 
2013-07-09T02:22:18.535639Z 
2013-07-09T02:22:22.746751Z 
+0

所以你只需要在時間戳列表之前打印'WAM'文件? 'echo'WAM「> foo有什麼問題; awk'/ \ WAM/{print $ 1「\ t」}'/ home/santosh/messages >> foo'? – abarnert

+0

我需要爲不同的數據導出excel表格 – santy

+0

對於不同的數據,你在問題中向我們展示了不同的數據?任何數據,特別是,或者你只是想隨機一些數據? – abarnert

回答

3

試試這個:

awk 'BEGIN{print "WAM"}/\<WAM\>/{print $1}' /home/santosh/messages > text.file 
1

嘗試

awk '/\ WAM/ {print $1"\t"}' /home/santosh/messages > ~/testOutput 

您可以隨時導入爲由Excel中的選項卡分隔的文件。

1

BASH:

grep WAM /var/log/messages | cut -d' ' -f1 > /home/santosh/messages 

的Python:

with open('/var/log/messages') as infile, open('/home/santosh/messages', 'w') as outfile: 
    outfile.write("WAM\n") 
    for line in (line for line in infile if "WAM" in line): 
     outfile.write(line.partition(' ')[0] 
     outfile.write('\n') 
2

嘗試這一個班輪:

awk 'BEGIN{v="WAM";print v}$NF==v&&$0=$1' yourfile 

用你的例子:

kent$ echo "2013-07-09T02:22:18.535639Z user.info WebAppMgr WAM 
2013-07-09T02:22:21.817372Z user.info sam SAM ^Icom.palm.app.calculator 
2013-07-09T02:22:21.818442Z user.info sam SAM ^Icom.palm.app.settings 
2013-07-09T02:22:22.746751Z user.info WebAppMgr WAM 
2013-07-09T02:22:23.846636Z user.info sam SAM ^Icom.palm.app.notes 
2013-07-09T02:22:24.851727Z user.info sam SAM ^Icom.palm.app.firstuse"|awk 'BEGIN{v="WAM";print v}$NF==v&&$0=$1' 
WAM 
2013-07-09T02:22:18.535639Z 
2013-07-09T02:22:22.746751Z