Posted By

lfatr on 02/16/10


Tagged

sort grep awk echo uniq


Versions (?)

SHELL SCRIPT PARSING LOGFILE


 / Published in: Bash
 

  1. #!/bin/bash
  2.  
  3. #initialize temp files
  4. echo "" > topimages.txt
  5. echo "" > images.txt
  6. echo "" > unique.txt
  7. echo "" > all.txt
  8.  
  9. if [ "$1" = "" ]; then
  10. echo -e "normally use like this:"
  11. echo -e " ./getTopPages.sh $1"
  12. echo -e " get all jpg's -- ./getTopPages.sh jpg"
  13. echo -e " get everything in photos -- ./getTopPages.sh photos"
  14. echo -e "*****"
  15. exit
  16. fi
  17.  
  18. #live server logfile
  19. logfile=/var/log/httpd/...
  20.  
  21. grep $1 $logfile| awk '{print $7}' | sort > all.txt
  22. echo found: $(grep '^.*$' all.txt -c) images total...
  23.  
  24. grep $1 $logfile| awk '{print $7}' | sort | uniq > unique.txt
  25. echo found: $(grep '^.*$' unique.txt -c) unique images...
  26.  
  27. echo '<images>' > images.txt
  28.  
  29. for i in $( grep '^.*$' unique.txt ); do
  30.  
  31. cnt=$( grep $i all.txt -c )
  32.  
  33. #add leading zeros
  34. if test $cnt -lt 10; then
  35. lzcnt=0000$cnt
  36. elif test $cnt -lt 100; then
  37. lzcnt=000$cnt
  38. elif test $cnt -lt 1000; then
  39. lzcnt=00$cnt
  40. elif test $cnt -lt 10000; then
  41. lzcnt=0$cnt
  42. elif test $cnt -lt 100000; then
  43. lzcnt=$cnt
  44. else
  45. lzcnt=$cnt
  46. fi
  47.  
  48. echo '<image count="'$lzcnt'" src="'$i'" />' >> images.txt
  49.  
  50. done
  51.  
  52. sort images.txt -r -k17 > topimages.xml
  53. echo '</images>' >> topimages.xml
  54.  
  55. echo removing temp files...
  56.  
  57. rm images.txt
  58. rm unique.txt
  59. rm all.txt
  60. rm topimages.txt

Report this snippet  

You need to login to post a comment.