-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
88 lines (72 loc) · 1.8 KB
/
main.go
File metadata and controls
88 lines (72 loc) · 1.8 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package main
import (
"bufio"
"strings"
"flag"
"fmt"
"os"
)
func main() {
var flip bool
flag.BoolVar(&flip, "f", false, "")
flag.BoolVar(&flip, "flip", false, "")
var trim bool
flag.BoolVar(&trim, "t", false, "")
flag.BoolVar(&trim, "trim", false, "")
var separator string
flag.StringVar(&separator, "s", "", "")
flag.StringVar(&separator, "separator", "", "")
flag.Parse()
if flag.NArg() < 2 {
flag.Usage()
os.Exit(1)
}
prefixFile, err := os.Open(flag.Arg(0))
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
suffixFile, err := os.Open(flag.Arg(1))
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
// use 'a' and 'b' because which is the prefix
// and which is the suffix depends on if we're in
// flip mode or not.
fileA := prefixFile
fileB := suffixFile
if flip {
fileA, fileB = fileB, fileA
}
a := bufio.NewScanner(fileA)
for a.Scan() {
// rewind file B so we can scan it again
fileB.Seek(0, 0)
b := bufio.NewScanner(fileB)
for b.Scan() {
aText := a.Text()
bText := b.Text()
if trim {
aText = strings.TrimSpace(aText)
bText = strings.TrimSpace(bText)
}
if flip {
fmt.Printf("%s%s%s\n", bText, separator, aText)
} else {
fmt.Printf("%s%s%s\n", aText, separator, bText)
}
}
}
}
func init() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Combine the lines from two files in every combination\n\n")
fmt.Fprintf(os.Stderr, "Usage:\n")
fmt.Fprintf(os.Stderr, " comb [OPTIONS] <prefixfile> <suffixfile>\n\n")
fmt.Fprintf(os.Stderr, "Options:\n")
fmt.Fprintf(os.Stderr, " -f, --flip Flip mode (order by suffix)\n")
fmt.Fprintf(os.Stderr, " -s, --separator <str> String to place between prefix and suffix\n")
fmt.Fprintf(os.Stderr, " -t, --trim Trim strings\n")
}
}