Go newbie here!
I am trying to put together a Go program that will parse a log file and return specific information on lines matched.
To give an example of what I am trying to achieve I would start with a log file that looks like this:
2019-09-30T04:17:02 - REQUEST-A
2019-09-30T04:18:02 - REQUEST-C
2019-09-30T04:19:02 - REQUEST-B
2019-09-30T04:20:02 - REQUEST-A
2019-09-30T04:21:02 - REQUEST-A
2019-09-30T04:22:02 - REQUEST-B
From here I would want to extract all "REQUEST-A" and either print the time the request occurred to the terminal or to a file.
I have tried using os.Open and scanner and I can use scanner.Text to log that it has found occurrence of my string, like so:
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
func main() {
request := 0
f, err := os.Open("request.log")
if err != nil {
fmt.Print("There has been an error!: ", err)
}
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
if strings.Contains(scanner.Text(), "REQUEST-A") {
request = request + 1
}
if err := scanner.Err(); err != nil {
}
fmt.Println(request)
}
}
But I am unsure of how to take this to use it to retrieve the information I am after. Normally I would use Bash for this but I thought I would branch out and see if I could use Go. Any advise would be appreciated.