0

可以说我有几个文件包含这个:

[srv] $ cat *.file.* | egrep -R -h -A 3 'is up'

interface0/1, is up
0 input errors, 18 CRC, 0 frame, 0 overrun, 0 ignored
0 output errors, 7299 collisions, 0 interface resets
--
interface0/2, is up
0 input errors, 0 CRC, 0 frame, 0 overrun, 0 ignored
0 output errors, 0 collisions, 0 interface resets
--
interface0/5, is up
669 input errors, 192 CRC, 0 frame, 0 overrun, 0 ignored
0 output errors, 4991 collisions, 0 interface resets
--
.
.
n..
interface0/n, is up
0 input errors, 0 CRC, 0 frame, 0 overrun, 0 ignored
0 output errors, 0 collisions, 0 interface resets
--

我想打印出计数器值 > 0 并且“已启动”的任何接口,但 interface resets 除外。当我使用 grep 时,我无法确定该行所属的接口:/

是否可以收集符合我标准的行?

4

2 回答 2

0

我认为应该这样做:

sed -n -e '/is up/ {;N;N;/is up.*[1-9].* 0 interface resets/p;}' *.file.*

于 2013-09-23T15:36:21.910 回答
0

如果您想获取包含这些行的文件,您可以直接使用将参数传递给 egrep 而不是 ad use -Hnot -h

egrep -R -H -A 3 'is up' *.file.*

更新:你可以试试这个脚本:

#!/usr/bin/env awk -f

/^interface[0-9]+\/[0-9]+, is up/ {
    split($0, a, /[,\/]/)
    if (a[2] > 0) {
        getline b
        getline c
        if (c ~ /0 interface resets$/) {
            printf "%s\n%s\n%s\n--\n", $0, b, c
        }
    }
}

用法:awk -f script.awk *.file.*

示例输出:

interface0/1, is up
0 input errors, 18 CRC, 0 frame, 0 overrun, 0 ignored
0 output errors, 7299 collisions, 0 interface resets
--
interface0/2, is up
0 input errors, 0 CRC, 0 frame, 0 overrun, 0 ignored
0 output errors, 0 collisions, 0 interface resets
--
interface0/5, is up
669 input errors, 192 CRC, 0 frame, 0 overrun, 0 ignored
0 output errors, 4991 collisions, 0 interface resets
--
于 2013-09-23T14:28:46.213 回答