2013-06-19 25 views
0

我想搜索許多.log文件以查找特定的錯誤。下面的shell代碼給了我,我要檢查它規定在最後5行「錯誤終止」每個.log文件行:從文本行提取並比較整數

for f in *.log; do 
    tail -n 5 "$f" | grep -q "Error termination" && tac "$f" | grep -m 1 "Step number"; 
done 

這導致輸出,如:

Step number 40 out of a maximum of 216 
Step number 17 out of a maximum of 192 
Step number 25 out of a maximum of 216 
Step number 192 out of a maximum of 192 
Step number 21 out of a maximum of 200 

每行對應於一個.log文件。現在我想比較這兩個整數並只在整數相同的情況下打印文件名。

+0

你可以發佈你的日誌文件的樣本(最後幾行)嗎?管道過多... –

回答

0

下面這行不正是我想要的東西,雖然有可能以較少的管道等一個更優雅的方式。

for f in *.log ; do 
    tail -n 5 "$f" | grep -q "Error termination" && tac "$f" | grep -m 1 "Step number" | grep -qo 'Step number \([0-9]*\) out of a maximum of \1' && echo "$f"; 
done 
2

通過AWK只是通過它,像:

| awk '$3==$NF' 
+0

'awk $ 3 == $ NF'? –

+0

是的,你是對的。固定。 – 2013-06-19 15:59:43

+0

有沒有可能通過awk代碼像grep的-q那樣僅返回True或False值? – tmartin