顶部像观众吃尾巴-fstream

我正在寻找一个命令行工具,使用一系列行(tail -f典型),并将它们计数为:tail -f /var/log/apache2/access.log | 切-d''-f1 | SOME_COMMAND并显示一个类似于顶部的视图:

52 xxx.xxx.xxx.xxx 12 xxx.xxx.xxx.xxx 6 xxx.xxx.xxx.xxx 2 xxx.xxx.xxx.xxx

它可以如此方便,例如与这个sh相关联:

#!/bin/sh # NCSA structure : #IP - - [DATE] "METHOD URL HTTP/VERSION" STATUS LENGTH "REFERER" "USER AGENT" QUERY="" while [ "$1" ] ; do case "$1" in ip) QUERY="$QUERY"'\1' ;; date) QUERY="$QUERY"'\4' ;; method) QUERY="$QUERY"'\5' ;; url) QUERY="$QUERY"'\6' ;; version) QUERY="$QUERY"'\7' ;; status) QUERY="$QUERY"'\8' ;; length) QUERY="$QUERY"'\9' ;; referer) QUERY="$QUERY"'\10' ;; # Does not work... useragent) QUERY="$QUERY"'\11' ;; # Does not work *) QUERY="$QUERY""$1" ;; esac shift done sed -r 's/^([^ ]+) ([^ ]+) ([^ ]+) \[([^]]+)] "([^ ]+) ([^"]+) HTTP\/([^"]+)" ([^ ]+) ([^ ]+) "([^"]+)" "([^"]+)"$/'"$QUERY"'/g' 

有了这个命令,我正在寻找和我的脚本,你可以做:猫somelog | ncsa.sh url | SOME_COMMAND,并得到您浏览的url'z,或referer,或你想要的顶部

(如果有人可以修复\ 10解释为\ 1,然后是0 …:p)的bug,

祝你有美好的一天 !

解决这个问题的程序的第一个版本在这里提交:

http://github.com/JulienPalard/logtop

你在寻找uniq -ctr吗?

 cat /var/log/apache2/access.log | cut -d' ' -f1 | uniq -c | tr -s "\n " " " 

uniq手册页:

 Filter adjacent matching lines from INPUT (or standard input), writing to OUTPUT (or standard output). -c, --count prefix lines by the number of occurrences 

tr man页面:

 Translate, squeeze, and/or delete characters from standard input, writing to standard output. -s, --squeeze-repeats replace each input sequence of a repeated character that is listed in SET1 with a single occurrence of that character 

为了让它sorting后裔:

 cat /var/log/apache2/access.log | cut -d' ' -f1 | uniq -c | sort -gr | tr -s "\n " " " 

输出的一个例子(我混淆了IP的):

  87 71.255.255.11 54 95.255.222.255 50 84.255.255.120 50 178.255.255.14 49 92.255.255.240 49 91.255.36.215 49 255.52.126.184 49 217.255.110.23 49 216.255.45.4 49 255.8.27.5 

注意:我的例子是使用cat,因为我不认为使用tail -f可以工作,因为没有End of File ,但是你可以使用tail -100作为例子,并且定期执行。