全栈编程

Balance $ 2,317
Item Sold 1230
文章作者: 全栈编程@luboke.com
版权声明: 本文章为go语言体系课视频教程配套电子书,版权归 全栈编程@luboke.com所有,欢迎免费学习,转载必须注明出处!但禁止任何商业用途,否则将受到法律制裁!
package main

import (
	"fmt"
	"os/exec"
	"strconv"
	"strings"
)

type CombineResult struct {
	urls string
	nums int64
}

var (
	cmd *exec.Cmd
	output []byte
	err error
	cmdstr string
	executeResult string
	combineResult []*CombineResult
	_combineResult *CombineResult
	nums int64
)

//【交易日志处理】
func main()  {
	cmdstr = `cd /Users/bobyandpp/luboke.com/logs/;grep -iE "\"(POST|GET)[[:blank:]](\S)+[[:blank:]]HTTP\/1\.[1|0]\"[[:blank:]]+\|[[:blank:]]+[[:digit:]]+[[:blank:]]+\|[[:blank:]]+[[:digit:]]+[[:blank:]]+\|[[:blank:]]+(\S)+" miniprograme.onajax.com.log  | awk -F " " 'BEGIN{record=0;}{arr[$6]++;record++}END{for(i in arr){print i,arr[i]}}' `


	// 生成Cmd
	cmd = exec.Command("/bin/bash", "-c", cmdstr)

	// 执行了命令, 捕获了子进程的输出( pipe )
	if output, err = cmd.CombinedOutput(); err != nil {
		fmt.Println("cmd.CombinedOutput()==",err.Error())
	}

	// 打印子进程的输出
	executeResult = string(output)



	//将执行的结果即字符串按换行符切割
	now := strings.Split(executeResult,"\n")
	for _,v := range now {
		_combineResult  = &CombineResult{}
		vvv := strings.Split(v," ")
		for kk,vv := range vvv {
			//fmt.Println("\t",kk,vv)
			if kk == 1 {
				if _combineResult.nums,err = strconv.ParseInt(vv,10,64);err != nil {
					fmt.Println(err.Error())
					continue
				}

				//_combineResult.nums = nums

			}
			if kk == 0 {
				fmt.Println(vv)
				_combineResult.urls = vv
			}
		}
		combineResult = append(combineResult,_combineResult)
	}


	for k,v := range combineResult {
		fmt.Println(k,v)
	}

}

查找指定微信支付通知的数据

package main

import (
	"bytes"
	"encoding/xml"
	"fmt"
	"io"
	"log"
	"os/exec"
	"strconv"
	"strings"
)

type CombineResult struct {
	ips string
	metho string
	protocol string
	status int64
	size int64
	content string
}

var (
	cmd *exec.Cmd
	output []byte
	err error
	cmdstr string
	executeResult string
	combineResult []*CombineResult
	_combineResult *CombineResult
	nums int64
)

//【交易日志处理】
func main()  {
	cmdstr = `cd /Users/bobyandpp/luboke.com/logs/;grep -iE "\"(POST|GET)[[:blank:]](\S)+[[:blank:]]HTTP\/1\.[1|0]\"[[:blank:]]+\|[[:blank:]]+[[:digit:]]+[[:blank:]]+\|[[:blank:]]+[[:digit:]]+[[:blank:]]+\|[[:blank:]]+(<xml><appid>\S)+" miniprograme.onajax.com.log  | awk -F " " 'BEGIN{i=0;}{i++;print $1,$5,$7,$9,$11,$13}END{}' `


	// 生成Cmd
	cmd = exec.Command("/bin/bash", "-c", cmdstr)

	// 执行了命令, 捕获了子进程的输出( pipe )
	if output, err = cmd.CombinedOutput(); err != nil {
		fmt.Println("cmd.CombinedOutput()==",err.Error())
	}

	// 打印子进程的输出
	executeResult = string(output)
	//fmt.Println(executeResult)


	//将执行的结果即字符串按换行符切割
	now := strings.Split(executeResult,"\n")
	for _,v := range now {
		_combineResult  = &CombineResult{}
		vvv := strings.Split(v," ")
		for kk,vv := range vvv {
			fmt.Println("\t",kk,vv)
			if kk == 0 {
				_combineResult.content = vv
			}

			if kk == 1 {
				_combineResult.metho = vv
			}

			if kk == 2 {
				_combineResult.protocol = vv
			}

			if kk == 3 {
				_combineResult.status,err = strconv.ParseInt(vv,10,10)
			}

			if kk == 4 {
				_combineResult.size,err = strconv.ParseInt(vv,10,10)
			}

			if kk == 5 {
				_combineResult.content = vv
			}

		}
		combineResult = append(combineResult,_combineResult)
	}

	var result map[string]interface{}
	for k,v := range combineResult {
		//fmt.Println(k,v.ips,v.metho,v.protocol,v.status,v.size)
		fmt.Println(k,v.content)
		result = XmlToMap([]byte(v.content))
		for k,v := range result {
			log.Println(k,v)
		}
	}

}


func XmlToMap(xmlData []byte) map[string]interface{} {
	decoder := xml.NewDecoder(bytes.NewReader(xmlData))
	m := make(map[string]interface{})
	var token xml.Token
	var err error
	var k string
	for token, err = decoder.Token(); err == nil; token, err = decoder.Token() {
		if v, ok := token.(xml.StartElement); ok {
			k = v.Name.Local
			continue
		}
		if v, ok := token.(xml.CharData); ok {
			data := string(v.Copy())
			if strings.TrimSpace(data) == "" {
				continue
			}
			m[k] = data
		}
	}

	if err != nil && err != io.EOF {
		panic(err)
	}
	return m
}

 

 

文章作者: 全栈编程@luboke.com
版权声明: 本文章为go语言体系课视频教程配套电子书,版权归 全栈编程@luboke.com所有,欢迎免费学习,转载必须注明出处!但禁止任何商业用途,否则将受到法律制裁!
copyright © 2020 全栈编程@luboke.com