如何在cat和grep文件中选择今天的日期?

问题描述:

如何选择今天的记录来自:如何在cat和grep文件中选择今天的日期?

Oct 9 21:47:06 server dovecot[1513]: imap([email protected]): Disconnected: Logged out in=235 out=760 
Oct 9 21:47:06 server dovecot[1513]: auth-worker(28110): shadow([email protected],127.0.0.1): unknown user 
Oct 9 21:47:06 server dovecot[1513]: auth-worker(28110): shadow([email protected],127.0.0.1): unknown user 
Oct 9 21:47:06 server dovecot[1513]: imap-login: Login: user=<[email protected]>, method=PLAIN, rip=127.0.0.1, lip=127.0.0.1, mpid=1850, secured, session=<ImGl4XUEHAB/AAAB> 
Oct 8 21:47:06 server dovecot[1513]: imap([email protected]): Disconnected: Logged out in=162 out=7805 
Oct 8 21:47:08 server dovecot[1513]: auth-worker(28110): shadow([email protected],144.76.43.87): unknown user 
Oct 8 21:47:08 server dovecot[1513]: auth-worker(28110): shadow([email protected],144.76.43.87): unknown user 
Oct 7 21:47:08 server dovecot[1513]: imap-login: Login: user=<[email protected]>, method=PLAIN, rip=144.76.43.87, lip=144.76.43.87, mpid=1853, secured, session=<gkTD4XUE0QCQTCtX> 
Oct 6 21:47:08 server dovecot[1513]: imap([email protected]): Disconnected: Logged out in=235 out=765 
Oct 4 21:47:09 server dovecot[1513]: auth-worker(28110): shadow([email protected],127.0.0.1): unknown user 
Oct 4 21:47:09 server dovecot[1513]: auth-worker(28110): shadow([email protected],127.0.0.1): unknown user 
Oct 4 21:47:09 server dovecot[1513]: imap-login: Login: user=<[email protected]>, method=PLAIN, rip=127.0.0.1, lip=127.0.0.1, mpid=1856, secured, session=<sb/G4XUEIAB/AAAB> 

我的命令是:

cat /var/log/maillog | grep imap-login:\ Login | sed -e 's/.*Login: user=<\(.*\)>, method=.*/\1/g' | sort | uniq 

在流水线中不需要使用grep两次sed,因为它可以做的选择,太:

sed -n "/^$(date '+%b %_d').*imap-login: Login/s/.*Login: user=<\(.*\)>, method=.*/\1/p" /var/log/maillog | sort -u 

我还取消了单独的呼叫uniq因为sort -u需要的照顾。

我用圭多的date命令来选择当前日期,但我取代了过时反引号与$(),马克做了,这是由POSIX指定,所有现代的Bourne衍生弹的支持。

这是Mark Setchell的AWK答案的一个版本,它可以对结果进行排序和统一。

awk -F"[ <>=,]*" -v d="^$(date '+%b %_d')" '$0 ~ d && /imap-login/ {a[$9] = $9} END {n = asort(a); for (i = 1; i <= n; i++) {print a[i]}}' /var/log/maillog 

它需要GAWK。

+0

伟人,我希望成为像你这样的人:)谢谢 – 2014-10-11 13:37:58

你也许可以做的东西沿着这些路线与awk和治疗空间,尖括号,逗号和等号全部作为备用字段分隔符:

awk -F"[ <>=,]*" -v d="$(date '+%b %_d')" '$0 ~ d && /imap-login/{print $1,$2,$9,$11}' maillog 
Oct 9 [email protected] PLAIN