如何将grep最后几行的输出放到线上?

如何将grep最后几行的输出放到线上?

假设我有这个文件:

 Thu May  8 15:32:07 2014
        User-Name = "Mark"
        Framed-IP-Address = 0.0.0.0
        Acct-Status-Type = Interim-Update
        Acct-Input-Octets = 95684
        Acct-Output-Octets = 23564

    Thu May  8 15:32:07 2014
        User-Name = "Mike"
        Framed-IP-Address = 0.0.0.0
        Acct-Status-Type = Interim-Update
        Acct-Input-Octets = 95684
        Acct-Output-Octets = 23564
    Thu May  8 15:32:07 2014
        User-Name = "Mike"
        Framed-IP-Address = 0.0.0.0
        Acct-Status-Type = Interim-Update
        Acct-Input-Octets = 95684
        Acct-Output-Octets = 23564

    Thu May  8 15:32:07 2014
        User-Name = "Mark"
        Framed-IP-Address = 0.0.0.0
        Acct-Status-Type = Interim-Update
        Acct-Input-Octets = 95684
        Acct-Output-Octets = 23564

grep我已设法获取与使用with选项的用户相关的信息-A

grep -A4 "Mark" test
        User-Name = "Mark"
        Framed-IP-Address = 0.0.0.0
        Acct-Status-Type = Interim-Update
        Acct-Input-Octets = 95684
        Acct-Output-Octets = 23564
--
        User-Name = "Mark"
        Framed-IP-Address = 0.0.0.0
        Acct-Status-Type = Interim-Update
        Acct-Input-Octets = 95684
        Acct-Output-Octets = 23564

但我想要的输出是这样的:

User-Name = "Mark" 
            Acct-Input-Octets = 95684 95684
            Acct-Output-Octets = 23564 23564

正如我们注意到的,我想消除“Mark”之后的前两行Framed-IP-Address = 0.0.0.0Acct-Status-Type = Interim-Update并将同一字段的所有值放在同一行中?

作为沃里克建议,我的问题的第一部分可以很容易回答:

grep -A4 "Mark" test| grep -v Framed-IP-Address | grep -v Acct-Status-Type

请注意,这是一个示例,该文件可能包含许多用户名=“Mark”的部分,并且所需的输出将类似于:

User-Name = "Mark" 
                Acct-Input-Octets = val1 val2 val3 val4 .......
                Acct-Output-Octets = val1 val2 val3 val4 ........

答案1

search.awk

BEGIN {
    FS = "="
    cur_username = ""
}

$1 ~ /User-Name/ {
    cur_username = $2
    gsub(/^[ \t]+/, "", cur_username)
    gsub(/[ \t]+$/, "", cur_username)
}

$1 !~ /User-Name/ {
    if ((NF != 2) || (cur_username != searched_user))
        next

    key = $1
    gsub(/^[ \t]+/, "", key)
    gsub(/[ \t]+$/, "", key)

    value = $2
    gsub(/^[ \t]+/, "", value)
        gsub(/[ \t]+$/, "", value)

    values[key] = values[key] " " value
}

END {
    printf("User-Name = %s\n", searched_user)
    for(key in values) {
        printf("\t%s =%s\n", key, values[key])
    }
}

测试运行:

$ awk -f search.awk -v 'searched_user="Mark"' input 
User-Name = "Mark"
    Acct-Status-Type = Interim-Update Interim-Update
    Acct-Input-Octets = 95684 95684
    Framed-IP-Address = 0.0.0.0 0.0.0.0
    Acct-Output-Octets = 23564 23564

奖金 -group.awk用于对所有记录进行分组(太糟糕了诺克没有asorti):

BEGIN {
    FS = "="
    cur_username = ""
}

$1 ~ /User-Name/ {
    cur_username = $2
    gsub(/^[ \t]+/, "", cur_username)
    gsub(/[ \t]+$/, "", cur_username)
}

$1 !~ /User-Name/ {
    if (NF != 2)
        next

    key = $1
    gsub(/^[ \t]+/, "", key)
    gsub(/[ \t]+$/, "", key)

    value = $2
    gsub(/^[ \t]+/, "", value)
        gsub(/[ \t]+$/, "", value)

    users[cur_username,key] = users[cur_username,key] " " value
}

END {
    n = asorti(users, sorted)
    prev_username = ""
    for (i=1; i<=n; i++) {
        username_key = sorted[i]
        split(username_key, a, SUBSEP)
        username = a[1]
        key = a[2]
        value = users[sorted[i]]
        if (username != prev_username) {
            printf("User-Name = %s\n", username)
            prev_username = username
        }
        printf("\t%s =%s\n", key, value)
    }
}

测试运行:

$ gawk -f group.awk input 
User-Name = "Mark"
    Acct-Input-Octets = 95684 95684
    ...
User-Name = "Mike"
    Acct-Input-Octets = 95684 95684
    ...

答案2

你可以做类似的事情

awk -vRS= -F'\n' '$2 ~ /Mark/ {
  vals["User-Name"] = "Mark"
  for (i=5;i<=NF;i++) {
    split($i,a," = ");
    vals[a[1]]=sprintf("%s %s", vals[a[1]], a[2]);
  }
}     
END{for (i in vals) print i,"=",vals[i];}' test

test对于你的文件来说,它给出了

User-Name = Mark
        Acct-Input-Octets =  95684 95684
        Acct-Output-Octets =  23564 23564

答案3

  1. 过滤所需的段落,以收集有关该用户的数据。
  2. 过滤包含所需键(Acct-Input-OctetsAcct-Output-Octets)的行。如果间距不一致,您可能需要在此阶段标准化间距。
  3. 按键对条目进行排序(使用稳定排序,除非您不关心值的顺序)。
  4. 折叠相同键的序列。
awk -v RS= '/User-Name = "Mark"/' |
grep -E 'Acct-(Input|Output)-Octets *=' |
sort -k1,1 -s |
awk '
  BEGIN {printf "User-Name = \"Mark\""}
  $1 == key { printf " %s", $3; }
  $1 != key { key = $1; printf "\n%s", $0; }
  END { print "" }'

相关内容