zoukankan      html  css  js  c++  java
  • go语言实现筛选数据

    package main
    
    import (
        "bufio"
        "fmt"
        "io"
        "os"
        "regexp"
        "strings"
    )
    
    func main() {
        fileName := "log.log"
        file, err := os.OpenFile(fileName, os.O_RDWR, 0666)
        if err != nil {
            fmt.Println("Open file error!", err)
            return
        }
        defer file.Close()
    
        stat, err := file.Stat()
        if err != nil {
            panic(err)
        }
    
        var size = stat.Size()
        fmt.Println("file size=", size)
        buf := bufio.NewReader(file)
        f, err := os.Create("筛选的数据.txt")
        defer f.Close()
        for {
            line, err := buf.ReadString('
    ')
            line = strings.TrimSpace(line)
            go findLine(line)
            if err != nil {
                if err == io.EOF {
                    fmt.Println("File read ok!")
                    break
                } else {
                    fmt.Println("Read file error!", err)
                    return
                }
            }
        }
    }
    func findLine(line string) {
        str := line
        ipret := regexp.MustCompile(`([0-9]{1,3}.){3}[0-9]{1,3}`)
        timeret := regexp.MustCompile(`[(.+)]`)
        articleret := regexp.MustCompile(`http://www.neusoft.com/article/[0-9]{1,4}`)
        videoret := regexp.MustCompile(`http://www.neusoft.com/video/[0-9]{1,4}`)
        ip := ipret.FindAllStringSubmatch(str, -1)
        time := timeret.FindAllStringSubmatch(str, -1)
        article := articleret.FindAllStringSubmatch(str, -1)
        video := videoret.FindAllStringSubmatch(str, -1)
        f, err := os.OpenFile("筛选的数据.txt", os.O_WRONLY, 0644)
        if err != nil {
            fmt.Println("cacheFileList.yml file create failed. err: " + err.Error())
        } else {
            /*fmt.Println("all:", alls)*/ /*fmt.Println("all:", alls)*/
            if ip != nil {
    
                n, _ := f.Seek(0, os.SEEK_END)
                // 从末尾的偏移量开始写入内容
                _, err = f.WriteAt([]byte("ip:	"+ip[0][0]+"/t"), n)
                fmt.Printf("ip:%s	", ip[0][0])
            }
    
            for _, one := range time {
                n, _ := f.Seek(0, os.SEEK_END)
                // 从末尾的偏移量开始写入内容
                _, err = f.WriteAt([]byte("time:	"+one[0]+"/t"), n)
                fmt.Printf("时间:%s	", one[0])
            }
            for _, one := range article {
                n, _ := f.Seek(0, os.SEEK_END)
                // 从末尾的偏移量开始写入内容
                _, err = f.WriteAt([]byte("article:	"+one[0]+"/t"), n)
                fmt.Printf("时间:%s	", one[0])
            }
            for _, one := range video {
                n, _ := f.Seek(0, os.SEEK_END)
                // 从末尾的偏移量开始写入内容
                _, err = f.WriteAt([]byte("video:	"+one[0]+"/t"), n)
                fmt.Printf("时间:%s	", one[0])
            }
            n, _ := f.Seek(0, os.SEEK_END)
            _, err = f.WriteAt([]byte("
    "), n)
        }
    }
    https://necydcy.me/
  • 相关阅读:
    HTML元素解释
    Java命名规范
    HDU 1058 Humble Numbers(DP,数)
    HDU 2845 Beans(DP,最大不连续和)
    HDU 2830 Matrix Swapping II (DP,最大全1矩阵)
    HDU 2870 Largest Submatrix(DP)
    HDU 1421 搬寝室(DP)
    HDU 2844 Coins (组合背包)
    HDU 2577 How to Type(模拟)
    HDU 2159 FATE(二维完全背包)
  • 原文地址:https://www.cnblogs.com/miria-486/p/10066916.html
Copyright © 2011-2022 走看看