Skip to content

Instantly share code, notes, and snippets.

@jemygraw
Last active December 14, 2016 09:58
Show Gist options
  • Save jemygraw/8fb31bdf98fae1ca479df01d47ff3ab8 to your computer and use it in GitHub Desktop.
Save jemygraw/8fb31bdf98fae1ca479df01d47ff3ab8 to your computer and use it in GitHub Desktop.
split the cdn access log items
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
/*
223.247.160.139 - 32 [11/Dec/2016:23:43:04 +0800] "GET http://static3.hifun.mobi/video/2016/11/11/dc8d031b7fcdf0801509b9af04d6bb8b.mp4?vframe/jpg/offset/1/w/0/h/0/rotate/auto HTTP/1.1" 200 20469 "-" "Dalvik/2.1.0 (Linux; U; Android 6.0; Letv X500 Build/DBXCNOP5801810092S)"
*/
func main() {
input := os.Stdin
bScanner := bufio.NewScanner(input)
for bScanner.Scan() {
line := bScanner.Text()
split(line, " ")
}
}
func split(line string, delimiter string) {
if line == "" {
//just ignore empty line
return
}
items := strings.SplitN(line, delimiter, 2)
val := strings.Trim(items[0], "[]\"")
fmt.Print(val)
next := strings.TrimSpace(items[1])
if next != "" {
fmt.Print("\t")
//fmt.Println(next)
}
if strings.HasPrefix(next, "[") {
split(next[1:], "]")
} else if strings.HasPrefix(next, "\"") {
split(next[1:], "\"")
} else {
split(next, " ")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment