-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
136 lines (119 loc) · 2.82 KB
/
main.go
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package main
import (
"encoding/csv"
"flag"
"fmt"
"io"
"os"
"strconv"
"strings"
)
const DefaultDelimiter = ","
type Options struct {
Reader io.Reader
Delimiter rune
NoHeaders bool
Field int
Check string
Select []int
}
var (
options *Options = new(Options)
delim string
field string
selection string
splitSelection []string
)
func init() {
flag.StringVar(&delim, "d", DefaultDelimiter, "CSV file delimiter character")
flag.BoolVar(&options.NoHeaders, "no-headers", false, "Indicate that this CSV file has no headers")
flag.StringVar(&selection, "s", "", "Fields to output (comma separated)")
flag.Parse()
// Extract delimeter
for _, d := range delim {
options.Delimiter = d
break
}
splitSelection = strings.Split(selection, DefaultDelimiter)
if flag.NArg() == 2 {
field = flag.Arg(0)
options.Check = flag.Arg(1)
options.Reader = os.Stdin
} else if flag.NArg() >= 3 {
file, err := os.Open(flag.Arg(0))
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
options.Reader = file
field = flag.Arg(1)
options.Check = flag.Arg(2)
} else {
fmt.Fprintf(os.Stderr, "%s\n", "Missing arguments")
os.Exit(1)
}
options.Select = make([]int, 0, len(splitSelection))
if options.NoHeaders {
fieldIndex, err := strconv.Atoi(field)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", "Field must be a number")
os.Exit(1)
}
options.Field = fieldIndex
for _, selectionName := range splitSelection {
selectionIndex, err := strconv.Atoi(selectionName)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", "Selection can only contain numbers")
os.Exit(1)
}
options.Select = append(options.Select, selectionIndex)
}
}
}
func main() {
var err error
reader := csv.NewReader(options.Reader)
reader.ReuseRecord = true
reader.Comma = options.Delimiter
if !options.NoHeaders {
headers, err := reader.Read()
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
for n, header := range headers {
if header == field {
options.Field = n
}
for i, selectionName := range splitSelection {
if header == selectionName {
if i < len(options.Select) {
options.Select = append(options.Select, 0)
copy(options.Select[i+1:], options.Select[i:])
options.Select[i] = n
} else {
options.Select = append(options.Select, n)
}
}
}
}
}
// Now lets search
var value string
var record []string
var output []string = make([]string, 0, len(options.Select))
for {
record, err = reader.Read()
if err == io.EOF {
os.Exit(0)
}
value = record[options.Field]
if strings.Contains(value, options.Check) {
output = output[:0]
for _, i := range options.Select {
output = append(output, record[i])
}
fmt.Fprintln(os.Stdout, strings.Join(output, "\t"))
}
}
}