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('\n')
        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:\t"+ip[0][0]+"/t"), n)
            fmt.Printf("ip:%s\t", ip[0][0])
        }

        for _, one := range time {
            n, _ := f.Seek(0, os.SEEK_END)
            // 从末尾的偏移量开始写入内容
            _, err = f.WriteAt([]byte("time:\t"+one[0]+"/t"), n)
            fmt.Printf("时间:%s\t", one[0])
        }
        for _, one := range article {
            n, _ := f.Seek(0, os.SEEK_END)
            // 从末尾的偏移量开始写入内容
            _, err = f.WriteAt([]byte("article:\t"+one[0]+"/t"), n)
            fmt.Printf("时间:%s\t", one[0])
        }
        for _, one := range video {
            n, _ := f.Seek(0, os.SEEK_END)
            // 从末尾的偏移量开始写入内容
            _, err = f.WriteAt([]byte("video:\t"+one[0]+"/t"), n)
            fmt.Printf("时间:%s\t", one[0])
        }
        n, _ := f.Seek(0, os.SEEK_END)
        _, err = f.WriteAt([]byte("\n"), n)
    }
}

 

转载于:https://www.cnblogs.com/miria-486/p/10066916.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值