Last active
June 12, 2020 00:31
-
-
Save suapapa/79cb476cafb8185d2b54473b66849123 to your computer and use it in GitHub Desktop.
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 ( | |
"bufio" | |
"flag" | |
"fmt" | |
"log" | |
"os" | |
"path/filepath" | |
"regexp" | |
"strings" | |
) | |
var ( | |
outName string | |
filePairCnt int | |
printVer bool | |
ver = "0.0.3" | |
) | |
type tester struct { | |
fn string | |
val []string | |
} | |
type testerGroup []tester | |
func main() { | |
flag.StringVar(&outName, "o", "out.csv", "out name") | |
flag.IntVar(&filePairCnt, "n", -1, "file pair count") | |
flag.BoolVar(&printVer, "v", false, "print version and exit") | |
flag.Parse() | |
if printVer { | |
fmt.Println(ver) | |
os.Exit(0) | |
} | |
r := regexp.MustCompile(`(\S*)\s+(\S*)\s+(\S*)`) | |
var tg testerGroup | |
files := []string{} | |
if filePairCnt > 1 { | |
lr := []string{"L", "R"} | |
for i := 1; i <= filePairCnt; i++ { | |
for d := 0; d < 2; d++ { | |
fn := fmt.Sprintf("%d_%s.txt", i, lr[d]) | |
files = append(files, fn) | |
} | |
} | |
} else { | |
var err error | |
files, err = filepath.Glob("*.txt") | |
chk(err) | |
} | |
for _, fn := range files { | |
log.Printf("parsing txt, %s...", fn) | |
f, err := os.Open(fn) | |
chk(err) | |
t := tester{ | |
fn: strings.TrimSuffix(fn, ".txt"), | |
} | |
scanner := bufio.NewScanner(f) | |
scanner.Scan() // remove first line | |
for scanner.Scan() { | |
line := scanner.Text() | |
// var freq, db, phase string | |
var db string | |
words := r.FindStringSubmatch(line) | |
// freq = words[1] | |
db = words[2] | |
// phase = words[3] | |
t.val = append(t.val, db) | |
} | |
tg = append(tg, t) | |
f.Close() | |
} | |
log.Printf("writing csv, %s...", outName) | |
// log.Printf("%+v", tg) | |
f, err := os.Create(outName) | |
chk(err) | |
defer f.Close() | |
// print header | |
for i := 0; i < len(tg); i++ { | |
fmt.Fprintf(f, "%s,", tg[i].fn) | |
} | |
fmt.Fprintln(f) | |
// print value | |
for j := 0; j < len(tg[0].val); j++ { | |
for i := 0; i < len(tg); i++ { | |
fmt.Fprintf(f, "%s,", tg[i].val[j]) | |
} | |
fmt.Fprintln(f) | |
} | |
log.Println("all done") | |
} | |
func chk(err error) { | |
if err != nil { | |
panic(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment