根据grep模式自定义文件内容显示

时间:2011-01-27 05:24:48

标签: linux shell unix scripting

日志文件包含大量数据,并根据数据和时间进行排序。每个日志的大小可能会有所不同。

我想在日志文件中搜索特定模式,如果模式匹配,它应该在屏幕上显示该特定日志。

任何shell命令都会很明显。

日志文件示例: -

07/17/2008 10:24:12.323411 >00.23
Line   441 of xx file
Dest IP Address: 192.189.52.255           Source IP Address: 192.189.52.200 

 000:   0101   0600   4D8C   444C    0000   0000   C0BD   34C8
 008:   C0BD   34C9   C0BD   34C9    0000   0000   FFFF   FFFF


07/17/2008 10:24:12.323549 >000.000138
    Use req data

 000:   0231   7564   705F   7573    7272   6571   2073   6F63

07/17/2008 10:24:12.323566 >000.000017
Local 192.189.52.200  Port 68 : Remote 0.0.0.0         Port 0

 000:   012D                                                   .-               
 000:   0000   0000   000A   0002    000A   012D               ...........-    

   0: NULNUL NULNUL NULLF  NULSTX  NULLF  SOH -               

如果我搜索特定的IP地址192.189.52.200。它应该相应地显示整个事件日志,

07/17/2008 10:24:12.323566 >000.000017
Local 192.189.52.200  Port 68 : Remote 0.0.0.0         Port 0

 000:   012D                                                   .-               
 000:   0000   0000   000A   0002    000A   012D               ...........-    

   0: NULNUL NULNUL NULLF  NULSTX  NULLF  -               

3 个答案:

答案 0 :(得分:0)

您可以在-A[n]使用grep标记,其中n表示匹配后的行数。 e.g

grep -A6 '192.189.52.200' my.log

答案 1 :(得分:0)

这需要GNU AWK(gawk),因为使用正则表达式作为记录分隔符(RS)。

#!/usr/bin/awk -f
BEGIN {
    pattern = ARGV[1]
    delete ARGV[1]

    # could use --re-interval
    d = "[0-9]"
    RS = d d "/" d d "/" d d d d " " d d ":" d d ":" d d "[^\n]*\n"
}

NR > 1 && ($0 ~ pattern || rt ~ pattern) {
    print rt
    print $0
}

{
    rt = RT # save RT for next record
}

它不漂亮,但它有效。

像这样运行:

./script.awk regex logfile

示例:

$ ./script.awk 'C0BD|012D' logfile

07/17/2008 10:24:12.323411 >00.23

Line   441 of xx file
Dest IP Address: 192.189.52.255           Source IP Address: 192.189.52.200

 000:   0101   0600   4D8C   444C    0000   0000   C0BD   34C8
 008:   C0BD   34C9   C0BD   34C9    0000   0000   FFFF   FFFF



07/17/2008 10:24:12.323566 >000.000017

Local 192.189.52.200  Port 68 : Remote 0.0.0.0         Port 0

 000:   012D                                                   .-
 000:   0000   0000   000A   0002    000A   012D               ...........-

   0: NULNUL NULNUL NULLF  NULSTX  NULLF  SOH -

$ ./script.awk '10:24:12.323549' logfile
07/17/2008 10:24:12.323549 >000.000138

    Use req data

 000:   0231   7564   705F   7573    7272   6571   2073   6F63

答案 2 :(得分:0)

如果您有Ruby或安装它的可能性,您可以编写一个脚本来解析日志文件并打印匹配条目。这是一个应该有效的脚本:

filename=ARGV[0]
regexpArg=ARGV[1]
unless filename and regexpArg
        puts "Usage: #{$0} <filename> <regexp>"
        exit(1)
end

dateStr='\d\d\/\d\d\/\d\d\d\d'
timeStr='[0-9:.]+'
whitespace='\s+'
regexpStr = dateStr + whitespace + timeStr + whitespace + '>[0-9.]+'
recordStart=Regexp.new(regexpStr)
records=[]
file=File.new(filename, "r")
addingToRecord = false
currentRecord = ""
file.each_line { |line|
        match = recordStart.match(line)
        if addingToRecord
                if match
                        records.push(currentRecord)
                        currentRecord = line
                else
                        currentRecord += line
                end
        else
                if match
                        addingToRecord = true
                        currentRecord = line
                end
        end
}
file.close
regexp=Regexp.new(regexpArg)
records.each { |r|
        if regexp.match(r)
                puts "----------------------------------------"
                puts r
                puts "----------------------------------------"
        end
}
相关问题