0

我有带有以下参数的邮件日志文件

relay=mx3.xyz.com

relay=mx3.xyz.com

relay=mx1.xyz.com

relay=mx1.xyz.com

relay=mx2.xyz.com

relay=home.xyz.abc.com

relay=127.0.0.1

我想计算除 127.0.0.1 之外的所有中继

输出应该是这样的

total relay= 6

mx3.xyz.com = 2

mx1.xyz.com = 2

mx2.xyz.com = 1

home.xyz.abc.com = 1
4

3 回答 3

3

如果您不介意使用 awk:

awk -F= '$2 != "127.0.0.1"  &&  /relay/ {count[$2]++; total++} 
  END { print "total relay = "total; 
        for (k in count) { print k" = " count[k]}
      }' maillog

你也可以只用uniqand grep,虽然你不会这样得到你的总数:

 grep relay maillog  | cut -d= -f2 | grep -v 127.0.0.1 | uniq -c

如果你不讨厌 perl:

perl -ne '/relay=(.*)/ and $1 ne "127.0.0.1" and ++$t and $h{$1}++;
  END {print "total = $t\n"; 
       print "$_ = $h{$_}\n" foreach keys %h;
  }' maillog
于 2013-02-07T10:02:45.097 回答
2

干得好:

 awk -F= '$2!="127.0.0.1"&&$2{t++;a[$2]++} END{print "total relay="t; for(x in a)print x"="a[x]}' yourfile

输出将是:

total relay=6
mx2.xyz.com=1
mx1.xyz.com=2
mx3.xyz.com=2
home.xyz.abc.com=1
于 2013-02-07T10:03:35.837 回答
0

我肯定会为此使用 awk (@Faiz 的回答)。但是我制定了这个令人痛苦的管道

cut -d= -f2 filename | grep -v -e '^[[:space:]]*$' -e 127.0.0.1 | sort | uniq -c | tee >(echo "$(bc <<< $(sed -e 's#[[:alpha:]].\+$#+#' -e '$a0')) total") | sed 's/^ *\([0-9]\+\) \(.*\)/\2 = \1/' | tac

输出

total = 6
mx3.xyz.com = 2
mx2.xyz.com = 1
mx1.xyz.com = 2
home.xyz.abc.com = 1

请不要赞成这个答案;)

于 2013-02-07T14:29:21.600 回答