-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog-records.go
73 lines (58 loc) · 1.48 KB
/
log-records.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
66
67
68
69
70
71
72
73
package main
import (
"bufio"
"fmt"
"os"
"strings"
"time"
)
// Function to process file and return tagMap and portProtocolMap
func ProcessFile(filename string) (map[string]int, map[string]int, error) {
tagMap := make(map[string]int)
portProtocolMap := make(map[string]int)
lookups, err := os.Open(filename)
if err != nil {
return nil, nil, err
}
defer lookups.Close()
lookupReader := bufio.NewReader(lookups)
for {
line, err := lookupReader.ReadString('\n')
if err != nil {
break
}
trimmedS := strings.TrimSpace(line)
splitLine := strings.Split(trimmedS, ",")
var port, protocol, tag string
port = strings.ToLower(strings.TrimSpace(splitLine[0]))
protocol = strings.ToLower(strings.TrimSpace(splitLine[1]))
if len(splitLine) == 2 {
tag = "untagged"
}
if len(splitLine) == 3 {
tag = strings.ToLower(strings.TrimSpace(splitLine[2]))
}
if tag != "" {
tagMap[tag]++
}
key := port + "," + protocol
if key != "," {
portProtocolMap[key]++
}
}
return tagMap, portProtocolMap, nil
}
// Main function calls the ProcessFile function
func main() {
start := time.Now()
args := os.Args
filename := args[1]
tagMap, portProtocolMap, err := ProcessFile(filename)
if err != nil {
fmt.Printf("unable to open file %s: %v", filename, err)
return
}
fmt.Println("Count of matches for each tag: ", tagMap)
fmt.Println("Count of matches for each port/protocol combination: ", portProtocolMap)
fmt.Println("Time elapsed: ", time.Until(start))
}