2013-02-19 67 views
-1

我有一個包含以下信息的日誌文件。我需要解析它得到一些信息。我如何使用grep來獲取這些信息或其他方法?解析日誌文件以獲取特定信息

connection= 5,size=262144,put=10 get=0 
swift-bench 2013-02-14 16:29:34,913 INFO Auth version: 1.0 
swift-bench 2013-02-14 16:29:36,580 INFO Auth version: 1.0 
swift-bench 2013-02-14 16:29:36,909 INFO 10 PUTS **FINAL** [0 failures], 30.6/s 
swift-bench 2013-02-14 16:29:36,910 INFO Auth version: 1.0 
swift-bench 2013-02-14 16:29:37,028 INFO 10 DEL **FINAL** [0 failures], 86.3/s 

所需的輸出:

Connection,size,put,gets,operation,op/s 
5,262144,10,0,PUTS,30.6 
5,262144,10,0,DEL,86.3 

回答

1

一個使用perl方式:

內容的 script.pl

#!/usr/bin/env perl 

use warnings; 
use strict; 

my $nums; 
while (<>) { 
    if ($. == 1) { 
     my @fields = m/(\w+)=/g; 
     push @fields, qw<operation op/s>; 
     printf qq|%s\n|, join q|,|, @fields; 

     $nums = join q|,|, m/=\s*(\d+)/g; 

     next; 
    } 

    my @f = split; 
    if ($f[5] !~ /(?i)version/ and @f > 7) { 
     printf qq|%s\n|, join q|,|, $nums, $f[5], substr($f[ $#f ], 0, length($f[ $#f ]) - 2); 
    } 
} 

而且asumming infile與貼在問題數據,運行它像:

perl script.pl infile 

國債收益率:

connection,size,put,get,operation,op/s 
5,262144,10,0,PUTS,30.6 
5,262144,10,0,DEL,86.3 
+0

使用在模式匹配(M //)未初始化值的,<>線2 ,提出,30.6 ,DEL,86.3 它給我錯誤上述輸出「使用未經初始化的值「。我對perl知之甚少,所以無法弄清楚什麼是錯的。感謝您的代碼。 – 2013-02-19 16:45:20

+0

而不是如果(。$ == 1),我使用如果(/連接/),它工作正常。謝謝!!!!! – 2013-02-19 17:38:58

0
#!/bin/bash 
conn=`grep -P -o -e '\d+(?=,size)' logfile` 
size=`grep -P -o -e '(?<=size\=)\d+' logfile` 
put=`grep -P -o -e '(?<=put\=)\d+' logfile` 
get=`grep -P -o -e '(?<=get\=)\d+' logfile` 
for i in `grep -P -e 'INFO \d' logfile | awk '{print $6","$10}' | tr -d '/s'`; do 
echo $conn,$size,$put,$get,$i 
done 
1

好吧,如果你可以在數據統計所一致的格式如圖所示,這將通過玩弄花招與IFS和切碎排隊到做位置參數。假設日誌文件的名稱在命令行上。在parse_swift.pl線17

#!/bin/bash 

logfile=$1 

echo "Connection,size,put,gets,operation,op/s" 
tmpIFS="$IFS" # In case we want to restore IFS later 
IFS="$IFS,=" 
# Note that the read below isn't splitting up the line 
# so the content of IFS isn't a problem 
while read line ; do 
    set -- $line 
    case "$line" in 
     connection*) 
      conn="$2" size="$4" puts="$6" gets="$8" 
     ;; 
     swift-bench*' PUTS '*|swift-bench*' DEL '*) 
      shift 6 
      case "$line" in 
       *'**FINAL**'*) echo "$conn,$size,$puts,$gets,$1,$5" ;; 
       *) echo "$conn,$size,$puts,$gets,$1,$4" ;; 
      esac 
     ;; 
    esac 

done < "$logfile" 

IFS="$tmpIFS" # Not needed if this is the end of the script 
+0

謝謝!這也適用,但我有這樣的一些行「swift-bench 2013-02-14 16:29:56,677信息83 PUTS [0次失敗],39.8/s」。它是否在每個空間後切碎線?你如何計算每個空間之後的位置? – 2013-02-19 19:09:50

+0

我修改了它來處理這種格式 - 它與其他的一樣,但沒有** FINAL **,所以它只是一個「參數」,將時間移動到4美元而不是5美元。如果你看IFS的設置方式,你可以看到它將打破空格(原始IFS值的一部分),逗號和等號。這對於快速臺式線路來說不是必需的,但它是用於連接線路的。 (我沒有假設只有一行以「連接」開頭,所以我想要一致的解析所有行的方式。) – William 2013-02-21 20:11:48