0

我正在尝试编写一个批处理文件,用于从日志文件中读取和提取用户代理,我可以使用以下代码执行此操作,但我需要给出发出请求的浏览器的数字计数并使用 gnu-plot,绘制一个每个浏览器请求数量的条形图。我对浏览器请求有点卡住了,我们将不胜感激。

干杯。

 #!/bin/bash

 # All we're doing here is extracting the user agent field from the log file and 'piping' it through some other commands. The first sort is to # enable uniq to properly identify and count unique user agents. The final sort orders the result by number and name (both descending).

 awk -F\" '{print $6}' access.log | sort | uniq -c | sort -fr > extracteduseragents.txt
4

1 回答 1

1

要为浏览器获取更友好的名称,您可以将例如pybrowscapbrowscap.csv与来自http://tempdownloads.browserscap.com/index.php的文件一起使用。

然后你可以使用如下脚本sanitize_ua.py

#!/usr/bin/env python
import sys
from pybrowscap.loader.csv import load_file

browscap = load_file('browscap.csv')

for ua in sys.stdin:
    browser = browscap.search(ua)
    if browser:
        print "'{} {}'".format(browser.name(), browser.version())

并从命令行运行

awk -F\" '{print $6}' access.log | sort | python sanitize_ua.py | uniq -c | sort -fr

当然,之前搜索所有的用户代理uniq是非常低效的,但它应该显示工作原理。当然,您也可以编写一个 Python 脚本来完成所有处理。

于 2013-11-21T09:01:32.237 回答