从文本文件中打印范围模式中的行

时间:2013-06-27 06:49:50

标签: regex perl sed awk pattern-matching

我有文件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]"

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

2 个答案:

答案 0 :(得分: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 file
======================
Tue Jun 25 00:03:00 WIT 2013
======================
    INFO     name               work
    NORMAL   jobs.count         46
    INFO     jobs.1.type        request

答案 1 :(得分: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读取段落,只打印那些带有计数&gt; 0