1+ package main
2+
3+ import (
4+ "bufio"
5+ "flag"
6+ "fmt"
7+ "os"
8+ )
9+
10+ func main () {
11+ var flip bool
12+ flag .BoolVar (& flip , "f" , false , "" )
13+ flag .BoolVar (& flip , "flip" , false , "" )
14+
15+ var separator string
16+ flag .StringVar (& separator , "s" , "" , "" )
17+ flag .StringVar (& separator , "separator" , "" , "" )
18+
19+ flag .Parse ()
20+
21+ if flag .NArg () < 2 {
22+ flag .Usage ()
23+ os .Exit (1 )
24+ }
25+
26+ prefixFile , err := os .Open (flag .Arg (0 ))
27+ if err != nil {
28+ fmt .Fprintf (os .Stderr , "%s\n " , err )
29+ os .Exit (1 )
30+ }
31+
32+ suffixFile , err := os .Open (flag .Arg (1 ))
33+ if err != nil {
34+ fmt .Fprintf (os .Stderr , "%s\n " , err )
35+ os .Exit (1 )
36+ }
37+
38+ // use 'a' and 'b' because which is the prefix
39+ // and which is the suffix depends on if we're in
40+ // flip mode or not.
41+ fileA := prefixFile
42+ fileB := suffixFile
43+
44+ if flip {
45+ fileA , fileB = fileB , fileA
46+ }
47+
48+ a := bufio .NewScanner (fileA )
49+ for a .Scan () {
50+ // rewind file B so we can scan it again
51+ fileB .Seek (0 , 0 )
52+
53+ b := bufio .NewScanner (fileB )
54+ for b .Scan () {
55+ if flip {
56+ fmt .Printf ("%s%s%s\n " , b .Text (), separator , a .Text ())
57+ } else {
58+ fmt .Printf ("%s%s%s\n " , a .Text (), separator , b .Text ())
59+ }
60+ }
61+ }
62+
63+ }
64+
65+ func init () {
66+ flag .Usage = func () {
67+ fmt .Fprintf (os .Stderr , "Combine the lines from two files in every combination\n \n " )
68+ fmt .Fprintf (os .Stderr , "Usage:\n " )
69+ fmt .Fprintf (os .Stderr , " comb [OPTIONS] <prefixfile> <suffixfile>\n \n " )
70+ fmt .Fprintf (os .Stderr , "Options:\n " )
71+ fmt .Fprintf (os .Stderr , " -f, --flip Flip mode (order by suffix)\n " )
72+ fmt .Fprintf (os .Stderr , " -s, --separator <str> String to place between prefix and suffix\n " )
73+ }
74+ }
0 commit comments