2013-12-19 38 views
0

我正在嘗試創建一個腳本來通過某些特定模式的日誌greps來訪問,例如:「HelloWorld」。日誌位於不同的服務器上,我可以ssh。我已經能夠收集信息並輸出到文件中,但爲了更高效,我希望能夠對發現實例的次數進行編號/索引,同時仍顯示找到的模式。使用grep通過日誌查找模式並輸出到文件

這是我有:

#!/bin/bash 
ips=(*array containing the ip addresses of the servers*) 
for j in ${ips[*]} 
do 
     result=$(ssh [email protected]"$j" "grep -R -i 'HelloWorld' /user/home/doc/abc.log" 2>&1) 
     echo "$j has the following errors: 
$result" 
done 

這是能夠完成90%的我的任務,然而10%的是什麼,我真的我知道我可以使用intested |廁所-l來。計算實例的數量,但是我在輸出文件上丟失了模式。

有了這個代碼,這是我所得到的:

*ip_1* has the following errors: 
2013-12-19 06:37:16,941 HelloWorld error, invoking the handler 
2013-12-19 08:30:18,008 [WARN ] HelloWorld invoking error handler 
*ip_2* has the following errors: 
2013-12-19 13:37:16,941 [WARN ] HelloWorld invoking error handler 
2013-12-19 15:30:18,008 HelloWorld error, invoking the handler 

但是,下面是我想:

*ip_1* has the following errors: 
1-2013-12-19 06:37:16,941 HelloWorld error, invoking the handler 
2-2013-12-19 08:30:18,008 [WARN ] HelloWorld invoking error handler 
*ip_2* has the following errors: 
1-2013-12-19 13:37:16,941 [WARN ] HelloWorld invoking error handler 
2-2013-12-19 15:30:18,008 HelloWorld error, invoking the handler 

的數值指標,告訴我有多少實例每個服務器,是它可能?如果是這樣,任何人都可以幫助我?

回答

0

在for循環,我想有這樣的事情:

echo "$j has the following errors:" 
ssh [email protected]"$j" "grep -R -i 'HelloWorld' /user/home/doc/abc.log" | cat -n 

格式比你雖然指定的內容略有不同。如果你確實需要這種格式,你可以在「cat -n」之後添加以下內容。

| sed -r 's/^\s*([0-9]+)\s*/\1-/' 
+0

謝謝你,簡單的修復,它做我想要的 – CjRobin