Thursday, 15 January 2015

shell - How do I count grep results for a string, but specify exclusions? -



shell - How do I count grep results for a string, but specify exclusions? -

i have maillog file below parameters

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

i want count relay except 127.0.0.1

output should this

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

if don't mind using awk:

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

and create uniq , grep, though won't total way:

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

and if don't hate perl:

perl -ne '/relay=(.*)/ , $1 ne "127.0.0.1" , ++$t , $h{$1}++; end {print "total = $t\n"; print "$_ = $h{$_}\n" foreach keys %h; }' maillog

shell sorting count awk uniq

No comments:

Post a Comment