我希望 bash 浏览 apache2 错误日志文件并列出其所有行(类似于cat
),但它将计算文件中包含的每一行的次数,并仅列出不同的行及其总数。
例如,如果错误日志如下所示:
Error 1: file failed
Error 2: client failed
Error 3: server failed
Error 1: file failed
我想得到这样的回应:
[2] Error 1: file failed
[1] Error 2: client failed
[1] Error 3: server failed
答案1
使用sort
和uniq
:
sort file | uniq -c
如果希望输出按出现次数排序,请| sort -n
在末尾添加。
答案2
使用 awk:
$ awk '{a[$0]++}END{for (i in a)print "["a[i]"]",i;}' file
[1] Error 3: server failed
[2] Error 1: file failed
[1] Error 2: client failed
计算数组 a 中出现的次数。在 END 标签处,打印 a 的所有元素。
答案3
要使上述任何示例真正有意义,您需要从每一行中删除日期。假设您的日志中有一行如下所示:
[Wed Oct 24 10:17:17 2012] [notice] caught SIGTERM, shutting down
那么你可以做类似的事情:
tail -n 5000 /var/log/apache2/error_log |\
cut -d] -f2- |\
awk '{a[$0]++} END \
{for (i in a) {printf "%s\t%s\n", a[i], i;} }' |\
sort -n
这将产生类似以下内容的输出:
13 [error] [client 127.0.0.1] column: address_phonerename: phone, referer: http://l27/symfony/web/varelager/adminpanel
13 [error] [client 127.0.0.1] column: address_streetrename: street, referer: http://l27/symfony/web/varelager/adminpanel
13 [error] [client 127.0.0.1] column: address_zipcoderename: zipcode, referer: http://l27/symfony/web/varelager/adminpanel
13 [error] [client 127.0.0.1] column: user_activerename: active, referer: http://l27/symfony/web/varelager/adminpanel
13 [error] [client 127.0.0.1] column: user_idrename: id, referer: http://l27/symfony/web/varelager/adminpanel
13 [error] [client 127.0.0.1] column: user_nameFirstrename: nameFirst, referer: http://l27/symfony/web/varelager/adminpanel
13 [error] [client 127.0.0.1] column: user_nameLastrename: nameLast, referer: http://l27/symfony/web/varelager/adminpanel
13 [error] [client 127.0.0.1] column: user_usernamerename: username, referer: http://l27/symfony/web/varelager/adminpanel
56 [error] [client 127.0.0.1] PHP Parse error: syntax error, unexpected '}' in /var/www/localhost/htdocs/tmp/php/log/index.php on line 6, referer: http://l27/symfony/web/varelager/adminpanel
56 [error] [client 127.0.0.1] PHP Parse error: syntax error, unexpected ';' in /var/www/localhost/htdocs/tmp/php/log/index.php on line 9, referer: http://l27/symfony/web/varelager/adminpanel
300 [error] [client 192.168.0.105] column: address_cityrename: city, referer: http://192.168.0.100/symfony/web/varelager/adminpanel
300 [error] [client 192.168.0.105] column: address_idrename: address_id, referer: http://192.168.0.100/symfony/web/varelager/adminpanel
300 [error] [client 192.168.0.105] column: address_namerename: addr_name, referer: http://192.168.0.100/symfony/web/varelager/adminpanel
300 [error] [client 192.168.0.105] column: address_streetrename: street, referer: http://192.168.0.100/symfony/web/varelager/adminpanel
300 [error] [client 192.168.0.105] column: address_zipcoderename: zipcode, referer: http://192.168.0.100/symfony/web/varelager/adminpanel
300 [error] [client 192.168.0.105] column: user_activerename: active, referer: http://192.168.0.100/symfony/web/varelager/adminpanel
300 [error] [client 192.168.0.105] column: user_idrename: id, referer: http://192.168.0.100/symfony/web/varelager/adminpanel
300 [error] [client 192.168.0.105] column: user_nameFirstrename: nameFirst, referer: http://192.168.0.100/symfony/web/varelager/adminpanel
300 [error] [client 192.168.0.105] column: user_nameLastrename: nameLast, referer: http://192.168.0.100/symfony/web/varelager/adminpanel
300 [error] [client 192.168.0.105] column: user_usernamerename: username, referer: http://192.168.0.100/symfony/web/varelager/adminpanel
301 [error] [client 192.168.0.105] column: address_commentrename: comment, referer: http://192.168.0.100/symfony/web/varelager/adminpanel
301 [error] [client 192.168.0.105] column: address_countryrename: country, referer: http://192.168.0.100/symfony/web/varelager/adminpanel
301 [error] [client 192.168.0.105] column: address_emailrename: email, referer: http://192.168.0.100/symfony/web/varelager/adminpanel
301 [error] [client 192.168.0.105] column: address_phonerename: phone, referer: http://192.168.0.100/symfony/web/varelager/adminpanel
451 [error] [client 192.168.0.103] File does not exist: /var/www/localhost/htdocs/WebServices