forked from PacktPublishing/Mastering-Go-Second-Edition
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchangeDT.go
65 lines (59 loc) · 1.33 KB
/
changeDT.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"bufio"
"fmt"
"io"
"os"
"regexp"
"strings"
"time"
)
func main() {
arguments := os.Args
if len(arguments) == 1 {
fmt.Println("Please provide one text file to process!")
os.Exit(1)
}
filename := arguments[1]
f, err := os.Open(filename)
if err != nil {
fmt.Printf("error opening file %s", err)
os.Exit(1)
}
defer f.Close()
notAMatch := 0
r := bufio.NewReader(f)
for {
line, err := r.ReadString('\n')
if err == io.EOF {
break
} else if err != nil {
fmt.Printf("error reading file %s", err)
}
r1 := regexp.MustCompile(`.*\[(\d\d\/\w+/\d\d\d\d:\d\d:\d\d:\d\d.*)\] .*`)
if r1.MatchString(line) {
match := r1.FindStringSubmatch(line)
d1, err := time.Parse("02/Jan/2006:15:04:05 -0700", match[1])
if err == nil {
newFormat := d1.Format(time.Stamp)
fmt.Print(strings.Replace(line, match[1], newFormat, 1))
} else {
notAMatch++
}
continue
}
r2 := regexp.MustCompile(`.*\[(\w+\-\d\d-\d\d:\d\d:\d\d:\d\d.*)\] .*`)
if r2.MatchString(line) {
match := r2.FindStringSubmatch(line)
d1, err := time.Parse("Jan-02-06:15:04:05 -0700", match[1])
if err == nil {
newFormat := d1.Format(time.Stamp)
fmt.Print(strings.Replace(line, match[1], newFormat, 1))
} else {
notAMatch++
}
continue
}
}
fmt.Println(notAMatch, "lines did not match!")
}