Bash 正在分析日志文件以获取某些信息

Bash 正在分析日志文件以获取某些信息,bash,parsing,awk,grep,Bash,Parsing,Awk,Grep,我有一个包含以下信息的日志文件。我需要对它进行解析以获取一些信息。如何使用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 IN

我有一个包含以下信息的日志文件。我需要对它进行解析以获取一些信息。如何使用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

使用
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 );
    }   
}
这将产生:

connection,size,put,get,operation,op/s
5,262144,10,0,PUTS,30.6
5,262144,10,0,DEL,86.3
#/bin/bash
conn=`grep-P-o-e'\d+(?=,size)'日志文件`

size=`grep-P-o-e'(?

好吧,如果您可以指望数据的格式如图所示一致,那么这将通过使用IFS来实现,并将行切分为位置参数。假设日志文件的名称在命令行上

#!/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
!/bin/bash
日志文件=$1
echo“连接、大小、放置、获取、操作、操作”
tmpIFS=“$IFS”#以防我们以后要恢复IFS
IFS=“$IFS,=”
#请注意,下面的内容并不是将行拆分
#所以IFS的内容不是问题
边读边做
设置--$line
大写“$line”
连接*)
conn=“$2”size=“$4”put=“$6”get=“$8”
;;
快速工作台*‘放置’*|快速工作台*‘删除’*)
班次6
大写“$line”
*“**最终**”*)回显“$conn、$size、$puts、$gets、$1、$5”;;
*)echo“$conn、$size、$put、$get、$1、$4”;;
以撒
;;
以撒
完成<“$logfile”
IFS=“$tmpIFS”#如果这是脚本的结尾,则不需要

在parse_swift.pl第17行第2行的模式匹配(m/)中使用未初始化值,PUTS,30.6,DEL,86.3它给出了上面的输出,错误为“使用未初始化值”。我对perl了解不多,因此无法找出问题所在。谢谢你的代码。谢谢!!这也行得通,但我有这样一些行:“swift bench 2013-02-14 16:29:56677 INFO 83 PUTS[0失败],39.8/s”。它会在每个空格后切掉行吗?如何计算每个空格后的位置?我也对其进行了修改,以处理该格式-它与其他空格相同,但没有FINAL,因此缩短了一个“参数”,将时间从5美元改为4美元。如果查看IFS的设置方式,可以看到它将分隔空格(原始IFS值的一部分)、逗号和等号上的行。这对于swift工作台管线来说是不必要的,但对于连接管线来说是必要的。(我没有假设只有一行以“connection”开头,所以我想要一种解析所有行的一致方法。)
#!/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
#!/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