Created
December 5, 2021 09:52
-
-
Save diamondburned/ee90a5957f84570ddd3918cee39bf3ea to your computer and use it in GitHub Desktop.
Script to parse CGOWRAP_PROFILE
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/csv" | |
"errors" | |
"io" | |
"log" | |
"os" | |
"strconv" | |
) | |
func main() { | |
count := map[string]float64{} | |
var lines []string | |
f, err := os.Open(os.Args[1]) | |
if err != nil { | |
log.Fatalln(err) | |
} | |
defer f.Close() | |
r := csv.NewReader(f) | |
for { | |
line, err := r.Read() | |
if err != nil { | |
if errors.Is(err, io.EOF) { | |
break | |
} | |
log.Fatalln("cannot read CSV:", err) | |
} | |
f, err := strconv.ParseFloat(line[1], 64) | |
if err != nil { | |
log.Fatalln("invalid float:", err) | |
} | |
log.Println(f) | |
if _, ok := count[line[0]]; !ok { | |
lines = append(lines, line[0]) | |
} | |
count[line[0]] += f | |
} | |
w := csv.NewWriter(os.Stdout) | |
defer w.Flush() | |
for _, line := range lines { | |
err := w.Write([]string{ | |
truncate(line), | |
strconv.FormatFloat(count[line], 'f', -1, 64), | |
}) | |
if err != nil { | |
log.Fatalln("cannot write CSV:", err) | |
} | |
} | |
} | |
func truncate(text string) string { | |
// if len(text) < 50 { | |
return text | |
// } | |
// return text[:23] + "..." + text[len(text)-23:] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment