Reputation: 664
I am currently trying to extract all the sender domains from maillog
. I am able to do some of that with the below command but the output is not quite what I desired. What would be the best approach to retrieve a unique list of sender domains from maillog?
grep from= /var/log/maillog |
awk '{print $7}' |
sort | uniq -c | sort -n
Output:
1 from=<[email protected]>,
1 from=<[email protected]>,
2 from=<[email protected]>,
2 from=<[email protected]>,
12 reject:
666 from=<>,
Desired output:
test.com
app1.com
example.com
Upvotes: 1
Views: 1822
Reputation: 189387
See useless use of grep
; if you are using Awk anyway, you don't really need grep
at all.
awk '$7 ~ /from=.*@/{split($7, a, /@/); ++count[a[2]] }
END { for(dom in count) print count[dom], dom }' /var/log/maillog
Collecting the count
s in an associative array does away with the need to call sort
and uniq
, too. Obviously, if you don't care about the count, don't print count[dom]
at the end.
Upvotes: 1
Reputation: 11
This should give you the answer:
grep from= /var/log/maillog | awk '{print $7}' | grep -Po '(?=@).{1}\K.*(?=>)' | sort -n | uniq -c
... change last items to "| sort | uniq" to remove the counts.
References:
https://www.baeldung.com/linux/bash-remove-first-characters {1}\K use
Extract email addresses from log with grep or sed -Po grep function
Upvotes: 0