Comment utiliser bash pour compter le nombre total de certaines lignes dans un fichier?


1

Je veux que la bash passe par un fichier journal des erreurs apache2 et la liste de toutes ses lignes (similaire à cat ), mais de manière à compter combien de fois chaque ligne est contenue dans le fichier et à ne répertorier que les lignes DISTINCT avec un nombre total de lignes.

Ainsi, par exemple, si le journal des erreurs ressemble à ceci:

Error 1: file failed
Error 2: client failed
Error 3: server failed
Error 1: file failed

J'aimerais avoir cet écho:

[2] Error 1: file failed
[1] Error 2: client failed
[1] Error 3: server failed

L'utilisation de bash seul est-elle une exigence stricte?
Ярослав Рахматуллин

Réponses:


5

Utilisation sort et uniq:

sort file | uniq -c

Si vous souhaitez que la sortie soit triée en fonction du nombre d'occurrences, ajoutez | sort -n à la fin.


Je les utilise si souvent. couper, trier, uniq, wc ... magie +1
captain_G

2

Utilisation de 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

Comptez le no. d'occurrences dans un tableau a. Sur l’étiquette FIN, imprimez tous les éléments de a.


0

Pour que l'un des exemples cités soit vraiment significatif, vous devez supprimer les dates de chaque ligne. En supposant qu'une ligne dans votre journal ressemble à ceci:

[Wed Oct 24 10:17:17 2012] [notice] caught SIGTERM, shutting down

alors vous pourriez faire quelque chose comme:

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  

Cela produira une sortie similaire à:

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
En utilisant notre site, vous reconnaissez avoir lu et compris notre politique liée aux cookies et notre politique de confidentialité.
Licensed under cc by-sa 3.0 with attribution required.