计算正则表达式的唯一出现次数

我有一台
maillog的服务器,我想知道每个用户每小时发送多少封电子邮件.

目前我删除了我不需要的所有信息,但我无法计算每个唯一用户发送的电子邮件数量.

到目前为止我写的是:

 awk '{print $3, $7;}' ./maillog | sed '/from/!d' | sed 's/:[0-9][0-9]:[0-9][0-9] /:00 /g' | sed 's/from=<//g' | egrep '[a-zA-Z0-9]+\@[a-zA-Z0-9.-]+(org|net|com)' | uniq -c > output.txt

主要问题是(我相信)我在同一时间内多次找到同一个用户(我不想要).

Here是我需要得到的.请注意,我需要得到的只是一个例子,不是我应该拥有的正确输出.如果你执行我在我给你的文件上写的脚本,你将在同一小时内获得user25 2次,但不满足要求.

以下是有人建议的输出样本(非常长):

Jan 16 08:33:04 mail.knurledwidgets.example.org sendmail[3539]: q5c1SrFqkAZq9b: Milter: connect to filters
Jan 16 08:33:06 mail.knurledwidgets.example.org sendmail[3539]: q5c1SrFqkAZq9b: from=<user1@dont-cross-the-memes.example.com>, size=38065260, class=-30, nrcpts=1, msgid=<gnDSaYSEaP4Yk/.F0EhYbIYcihGO8Vd.dont-cross-the-memes.example.com>, proto=ESMTP, daemon=MTA-v6, relay=proton.dont-cross-the-memes.example.com [192.168.98.234]
Jan 16 08:33:06 mail.knurledwidgets.example.org sendmail[7734]: qqGjhufuNY5UJ: Milter: connect to filters
Jan 16 08:33:07 mail.knurledwidgets.example.org sendmail[8780]: qkwEbHuoJi40Lj: Milter: connect to filters
Jan 16 08:33:07 mail.knurledwidgets.example.org sendmail[8780]: qkwEbHuoJi40Lj: from=<user25@knurledwidgets.example.org>, size=36412443, class=-30, nrcpts=1, msgid=<w/7AIsHSy6.gkNTPlyyE55u.knurledwidgets.example.org>, proto=ESMTP, daemon=MTA-v6, relay=mail.knurledwidgets.example.org [10.0.0.20]
Jan 16 08:33:08 mail.knurledwidgets.example.org sendmail[7734]: qqGjhufuNY5UJ: from=<user6@stellar-patrol.example.com>, size=33411319, class=-30, nrcpts=1, msgid=<il/5SxUES9XwRhX.KfO6ywkQROALbnz.stellar-patrol.example.com>, proto=ESMTP, daemon=MTA-v6, relay=feinstein.stellar-patrol.example.com [192.168.73.3]
Jan 16 08:33:09 mail.knurledwidgets.example.org sendmail[3539]: q5c1SrFqkAZq9b: Milter accept: message
Jan 16 08:33:09 mail.knurledwidgets.example.org sendmail[8780]: qkwEbHuoJi40Lj: Milter accept: message
Jan 16 08:33:10 mail.knurledwidgets.example.org sendmail[7734]: qqGjhufuNY5UJ: Milter accept: message
Jan 16 08:33:12 mail.knurledwidgets.example.org sendmail[1618]: qhgKT0cN80gSX: Milter: connect to filters
Jan 16 08:33:13 mail.knurledwidgets.example.org sendmail[1618]: qhgKT0cN80gSX: from=<user25@knurledwidgets.example.org>, size=780642, class=-30, nrcpts=1, msgid=<hX49btAurMDDZlhWo.5RpGEJxQQilElvDgRpc3sw.knurledwidgets.example.org>, proto=ESMTP, daemon=MTA-v6, relay=mail.knurledwidgets.example.org [10.0.0.20]

以下是输出示例:

1 08:00 user10@yuhoo.example.com
1 08:00 user19@knurledwidgets.example.org
1 08:00 user1@beshonk.example.com
5 08:00 user27@knurledwidgets.example.org
1 09:00 user12@knurledwidgets.example.org
1 09:00 user17@knurledwidgets.example.org
1 09:00 user26@knurledwidgets.example.org
7 09:00 user27@knurledwidgets.example.org
2 09:00 user33@knurledwidgets.example.org
1 09:00 user42@knurledwidgets.example.org

还请解释你给出的答案,因为目的是学习不做练习.

感谢您的时间

最佳答案 在uniq之前的那种将给你计数:

awk '{print $3, $7;}' ./maillog | sed '/from/!d' | sed 's/:[0-9][0-9]:[0-9][0-9] /:00 /g' | sed 's/from=<//g' | egrep '[a-zA-Z0-9]+\@[a-zA-Z0-9.-]+(org|net|com)' | sort | uniq -c`

  1 08:00 user1@dont-cross-the-memes.example.com>,
  2 08:00 user25@knurledwidgets.example.org>,
  1 08:00 user6@stellar-patrol.example.com>,

见uniq –help:

Note: uniq does not detect repeated lines unless they are adjacent.
You may want to sort the input first, or use sort -u without uniq.
Also, comparisons honor the rules specified by LC_COLLATE.

点赞