Shell statistics PV, UV, independent IP

Source: Internet
Author: User
Shell statistics PV, UV, independent IP analysis logs every day can not afford to hurt, often need to give PV, UV, independent IP and other statistical values, using C/C ++, java can be written. The process is like this: read the file first, scan it row by row, put the value that can be marked into the data structure, and record the final result ,... shell statistics PV, UV, independent IP analysis logs every day can not afford to hurt, often need to give PV, UV, independent IP and other statistical values, using C/C ++, java can write data. The process is as follows: read the file first, scan the file row by row, put the value that can be marked into the data structure, and record the final result, in fact, Linux itself has a very powerful text processing function, you can use shell + some text gadgets to get results. The daily log analysis team is really sorry. we often need to provide statistics such as PV, UV, and independent IP addresses. C/C ++ and java can all be used to write logs. The process is like this, first, read the file, scan it row by row, put the value that can be marked into the data structure, and record the final result. In fact, Linux itself has a very powerful text processing function, you can use shell + some text gadgets to get the result. The access log file output by Nngix is as follows: the log file code 192.168.1.166--119272312 [05/Nov/2011: 16: 06: 59 + 0800] "GET/index.html HTTP/1.1" 200 370" http://192.168.1.201/ "" Chrome/15.0.874.106 ""-"192.168.1.166--119272312 [05/Nov/2011: 16: 06: 59 + 0800]" GET/poweredby.png HTTP/1.1 "200 3034" http://192.168.1.201/ "" Chrome/15.0.874.106 ""-"192.168.1.177--1007071650 [05/Nov/2011: 16: 06: 59 + 0800]" GET/favicon. ico HTTP/1.1 "404 3650"-"" Chrome/15.0.874.106 ""-"192.168.1.178--58565468 [05/Nov/2011: 16: 17: 40 + 0800] "GET/HTTP/1.1" 200 3700 "-" "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0) "-" 192.168.1.166--119272312 [05/Nov/2011: 16: 17: 40 + 0800] "GET/nginx-logo.png HTTP/1.1" 200 370" http://192.168.1.201/ "" Chrome/15.0.874.106 ""-"PV is very simple. it is generally used to count the number of accesses to a URL, for example, count the access times of/index.html. Shell code grep "/index.html"/var/log/nginx/access. what about log-c UV? based on the user ID (column 4), we first need to truncate the string and use the cut command to separate it with spaces.-d "", take the fourth column-f 4, and then sort the weights here. you need to use the uniq tool. the uniq speed is very fast, but based on the nearest weight, the previous one and the other one will also sort the weights, if there is a different interval between them, it will not work. Therefore, you must use the sort tool to sort the identifiers, and then use the uniq tool to sort the identifiers. We use pipeline symbolic links between them, finally, we use wc-l to output statistics. for example, we have accessed/index.html, which is also the uv of the page: Shell code grep "/index.html"/var/log/nginx/access. log | cut-d ""-f 4 | sort | uniq | wc-l independent IP address: Suppose we want to count the independent IP address of the entire site, so we don't need to use grep to match a specific page, just use cat output: Shell code cat/var/log/nginx/access. log | cut-d ""-f 1 | sort | uniq | wc-l all wood has powerful awk, which completes the basic statistical requirements :)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.