2014-10-09 65 views
1

如何選擇今天的記錄來自:如何在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 

回答

1

在流水線中不需要使用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

0

你也許可以做的東西沿着這些路線與awk和治療空間,尖括號,逗號和等號全部作爲備用字段分隔符:

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