0

我有文件 log.txt :

======================
Tue Jun 25 00:02:00 WIT 2013
======================


pool
    INFO     name               work
    INFO     threads.total      30
    INFO     threads.active     7
    INFO     threads.waiting    23
    INFO     jobs.queue.latency 0
    INFO     jobs.run.latency   139
    INFO     jobs.queue.tps     3
    INFO     jobs.start.tps     3
    INFO     jobs.finish.tps    3
    NORMAL   jobs.count         0
pool
    INFO     name               recv
    INFO     threads.total      40
    INFO     threads.active     27
    INFO     threads.waiting    13
    INFO     jobs.queue.latency 0
    INFO     jobs.run.latency   0
    INFO     jobs.queue.tps     0
    INFO     jobs.start.tps     0
    INFO     jobs.finish.tps    0
    NORMAL   jobs.count         0
pool
    INFO     name               send
    INFO     threads.total      10
    INFO     threads.active     0
    INFO     threads.waiting    10
    INFO     jobs.queue.latency 0
    INFO     jobs.run.latency   1
    INFO     jobs.queue.tps     5
    INFO     jobs.start.tps     5
    INFO     jobs.finish.tps    5
    NORMAL   jobs.count         0


======================
Tue Jun 25 00:03:00 WIT 2013
======================

pool
    INFO     name               work
    INFO     threads.total      30
    INFO     threads.active     15
    INFO     threads.waiting    15
    INFO     jobs.queue.latency 0
    INFO     jobs.run.latency   13
    INFO     jobs.queue.tps     1
    INFO     jobs.start.tps     0
    INFO     jobs.finish.tps    0
    NORMAL   jobs.count         46
    INFO     jobs.1.type        request

pool
    INFO     name               recv
    INFO     threads.total      40
    INFO     threads.active     27
    INFO     threads.waiting    13
    INFO     jobs.queue.latency 0
    INFO     jobs.run.latency   0
    INFO     jobs.queue.tps     0
    INFO     jobs.start.tps     0
    INFO     jobs.finish.tps    0
    NORMAL   jobs.count         0
pool
    INFO     name               send
    INFO     threads.total      10
    INFO     threads.active     0
    INFO     threads.waiting    10
    INFO     jobs.queue.latency 0
    INFO     jobs.run.latency   0
    INFO     jobs.queue.tps     0
    INFO     jobs.start.tps     0
    INFO     jobs.finish.tps    0
    NORMAL   jobs.count         0

我想要这样的结果:


======================
Tue Jun 25 00:03:00 WIT 2013
======================
    INFO     name               work
    NORMAL   jobs.count         46
    INFO     jobs.1.type        request

我已经使用:

cat log.txt |grep -B14 -A1 -i 'jobs.count         [1-9]\{2,\}' |
egrep -i "jobs.1.type|=|WIT|jobs.count         [1-9]"

如果名称不是“work”而是“send”或“recv”,那么“grep -B14”将无法获取日期。还有其他解决方案吗?另一方面,如果“jobs.count”不是“0”,我想要grep而不是get date =“Tue Jun 25 00:03:00 WIT 2013”​​,get name =“INFO name work”并获得工作类型“INFO jobs .1.类型请求”:

4

2 回答 2

3

GNU 的代码:

/WIT|INFO +name|jobs.1.type|jobs.count +[1-9]/{
if ($0 ~ /WIT/)
    {cheader=$0}
if ($0 ~ /INFO +name/)
    {cname=$0}
if ($0 ~ /jobs.count +[1-9]/)
    {ccount=$0}
if ($0 ~ /jobs.1.type/)
    {
    i++
    header[i]=cheader
    name[i]=cname
    count[i]=ccount
    type[i]=$0
    }
}
END {
    line="======================"
    for (a=1 ;a<=i ;a++)
    {
    if (count[a] != "")
        {
        print line
        print header[a]
        print line
        print name[a]
        print count[a]
        print type[a]
        }
    }
}

输出示例:

$awk -f c.awk 文件
=======================
2013 年 6 月 25 日星期二 00:03:00
=======================
    INFO 名称工作
    正常工作。计数 46
    INFO jobs.1.type 请求
于 2013-06-27T08:26:20.763 回答
3
awk '/WIT/ {date=$0} /name/ {printf "\n%s\n%s\n", date, $0} /count|type/' file | 
awk -v RS="" '{for (i=1; i<=NF; i++) if ($i~/count/ && $(i+1)>0) {print; break}}'
Tue Jun 25 00:03:00 WIT 2013
    INFO     name               work
    NORMAL   jobs.count         46
    INFO     jobs.1.type        request

第一个 awk 打印包含所需日期和字段的段落。
第二个 awk 读取段落,只打印计数 > 0 的段落。

于 2013-06-27T10:48:41.750 回答