• go正则解析log文件


    解析log:
    下载需要解析的log

    wget https://gist.githubusercontent.com/clanchun/2b5e07cda53718ccbf64f62fb31900c8/raw/64be7f018973717dd5faa7be2bfb817f50ed05bb/access.log

    package main
    
    import (
    	"bufio"
    	"io"
    	"os"
    	"fmt"
    	"regexp"
    	"strings"
    )
    
    var (
    	filePath string
    	logReg = `TCP_HIT/([0-9][0-9][0-9]) [0-9]+ [A-Z]+ http://c13.adrise.tv/04C0BF/v2/sources/content-owners/[0-9a-z]+/([0-9]+)/`
    	logMap = make(map[string]int)
    )
    
    func init() {
    	filePath =os.Args[1]
    }
    
    func processLog(contents string) {
    	re := regexp.MustCompile(logReg)
    	matches := re.FindAllStringSubmatch(contents, -1)
    	for _, m := range matches {
    		key := m[1]+ "_" +m[2]
    		count, ok := logMap [ key ]
    		if (ok) {
    			logMap [ key ] = count + 1
    		} else {
    			logMap [ key ] = 1
    		}
    	}
    }
    
    func readLine(fileName string, handler func(string)) error {
    	f, err := os.Open(fileName)
    	if err != nil {
    		return err
    	}
    	buf := bufio.NewReader(f)
    	for {
    		line, err := buf.ReadString('
    ')
    //TrimSpace 将删除 s 首尾连续的的空白字符 line = strings.TrimSpace(line) handler(line) if err != nil { if err == io.EOF { return nil } return err } } return nil } func main() { //filePath = "/data/access.log" if err := readLine(filePath, processLog); err != nil { panic(err) } for log:= range logMap { fmt.Println(log, "appear count: ", logMap [log]) } }

      

  • 相关阅读:
    SP1716 GSS3
    A Simple Problem with Integers题解
    P4528 [CTSC2008]图腾 题解
    P1498 南蛮图腾 题解
    P2024 [NOI2001]食物链 题解
    Windows编程 Windows程序的生与死(中)
    Windows编程 Windows程序的生与死(上)
    C#实现在注册表中保存信息
    沿路径动画(Animation Along a Path)
    倾斜动画(SkewTransform)
  • 原文地址:https://www.cnblogs.com/xingyunshizhe/p/11314909.html
Copyright © 2020-2023  润新知