forked from bitfield/script
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilters.go
240 lines (226 loc) · 7.16 KB
/
filters.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package script
import (
"bufio"
"bytes"
"fmt"
"io"
"os"
"os/exec"
"regexp"
"sort"
"strconv"
"strings"
)
// Column reads from the pipe, and returns a new pipe containing only the Nth
// column of each line in the input, where '1' means the first column, and
// columns are delimited by whitespace. Specifically, whatever Unicode defines
// as whitespace ('WSpace=yes'). If there is an error reading the pipe, the
// pipe's error status is also set.
func (p *Pipe) Column(col int) *Pipe {
return p.EachLine(func(line string, out *strings.Builder) {
columns := strings.Fields(line)
if col <= len(columns) {
out.WriteString(columns[col-1])
out.WriteRune('\n')
}
})
}
// Concat reads a list of filenames from the pipe, one per line, and returns a
// pipe which reads all those files in sequence. If there are any errors (for
// example, non-existent files), the pipe's error status will be set to the
// first error encountered, but execution will continue.
func (p *Pipe) Concat() *Pipe {
if p == nil || p.Error() != nil {
return p
}
var readers []io.Reader
p.EachLine(func(line string, out *strings.Builder) {
input, err := os.Open(line)
if err != nil {
p.SetError(err)
return
}
readers = append(readers, NewReadAutoCloser(input))
})
return p.WithReader(io.MultiReader(readers...))
}
// EachLine calls the specified function for each line of input, passing it the
// line as a string, and a *strings.Builder to write its output to. The return
// value from EachLine is a pipe containing the contents of the strings.Builder.
func (p *Pipe) EachLine(process func(string, *strings.Builder)) *Pipe {
if p == nil || p.Error() != nil {
return p
}
scanner := bufio.NewScanner(p.Reader)
output := strings.Builder{}
for scanner.Scan() {
process(scanner.Text(), &output)
}
err := scanner.Err()
if err != nil {
p.SetError(err)
}
return Echo(output.String())
}
// Exec runs an external command and returns a pipe containing the output. If
// the command had a non-zero exit status, the pipe's error status will also be
// set to the string "exit status X", where X is the integer exit status.
func (p *Pipe) Exec(s string) *Pipe {
if p == nil || p.Error() != nil {
return p
}
q := NewPipe()
args := strings.Fields(s)
cmd := exec.Command(args[0], args[1:]...)
cmd.Stdin = p.Reader
output, err := cmd.CombinedOutput()
if err != nil {
q.SetError(err)
}
return q.WithReader(bytes.NewReader(output))
}
// First reads from the pipe, and returns a new pipe containing only the first N
// lines. If there is an error reading the pipe, the pipe's error status is also
// set.
func (p *Pipe) First(lines int) *Pipe {
if p == nil || p.Error() != nil {
return p
}
scanner := bufio.NewScanner(p.Reader)
output := strings.Builder{}
for i := 0; i < lines; i++ {
if !scanner.Scan() {
break
}
output.WriteString(scanner.Text())
output.WriteRune('\n')
}
p.Close()
err := scanner.Err()
if err != nil {
p.SetError(err)
}
return Echo(output.String())
}
// Freq reads from the pipe, and returns a new pipe containing only unique lines
// from the input, prefixed with a frequency count, in descending numerical
// order (most frequent lines first). Lines with equal frequency will be sorted
// alphabetically. If there is an error reading the pipe, the pipe's error
// status is also set.
func (p *Pipe) Freq() *Pipe {
if p == nil || p.Error() != nil {
return p
}
var freq = map[string]int{}
p.EachLine(func(line string, out *strings.Builder) {
freq[line]++
})
type frequency struct {
line string
count int
}
var freqs = make([]frequency, 0, len(freq))
var maxCount int
for line, count := range freq {
freqs = append(freqs, frequency{line, count})
if count > maxCount {
maxCount = count
}
}
sort.Slice(freqs, func(i, j int) bool {
if freqs[i].count == freqs[j].count {
return freqs[i].line < freqs[j].line
}
return freqs[i].count > freqs[j].count
})
fieldWidth := len(strconv.Itoa(maxCount))
var output strings.Builder
for _, item := range freqs {
output.WriteString(fmt.Sprintf("%*d %s", fieldWidth, item.count, item.line))
output.WriteRune('\n')
}
return Echo(output.String())
}
// Join reads the contents of the pipe, line by line, and joins them into a
// single space-separated string. It returns a pipe containing this string. Any
// terminating newline is preserved.
func (p *Pipe) Join() *Pipe {
if p == nil || p.Error() != nil {
return p
}
result, err := p.String()
if err != nil {
return p
}
var terminator string
if strings.HasSuffix(result, "\n") {
terminator = "\n"
result = result[:len(result)-1]
}
output := strings.ReplaceAll(result, "\n", " ")
return Echo(output + terminator)
}
// Match reads from the pipe, and returns a new pipe containing only lines which
// contain the specified string. If there is an error reading the pipe, the
// pipe's error status is also set.
func (p *Pipe) Match(s string) *Pipe {
return p.EachLine(func(line string, out *strings.Builder) {
if strings.Contains(line, s) {
out.WriteString(line)
out.WriteRune('\n')
}
})
}
// MatchRegexp reads from the pipe, and returns a new pipe containing only lines
// which match the specified compiled regular expression. If there is an error
// reading the pipe, the pipe's error status is also set.
func (p *Pipe) MatchRegexp(re *regexp.Regexp) *Pipe {
return p.EachLine(func(line string, out *strings.Builder) {
if re.MatchString(line) {
out.WriteString(line)
out.WriteRune('\n')
}
})
}
// Reject reads from the pipe, and returns a new pipe containing only lines
// which do not contain the specified string. If there is an error reading the
// pipe, the pipe's error status is also set.
func (p *Pipe) Reject(s string) *Pipe {
return p.EachLine(func(line string, out *strings.Builder) {
if !strings.Contains(line, s) {
out.WriteString(line)
out.WriteRune('\n')
}
})
}
// RejectRegexp reads from the pipe, and returns a new pipe containing only
// lines which don't match the specified compiled regular expression. If there
// is an error reading the pipe, the pipe's error status is also set.
func (p *Pipe) RejectRegexp(re *regexp.Regexp) *Pipe {
return p.EachLine(func(line string, out *strings.Builder) {
if !re.MatchString(line) {
out.WriteString(line)
out.WriteRune('\n')
}
})
}
// Replace filters its input by replacing all occurrences of the string `search`
// with the string `replace`. If there is an error reading the pipe, the pipe's
// error status is also set.
func (p *Pipe) Replace(search, replace string) *Pipe {
return p.EachLine(func(line string, out *strings.Builder) {
out.WriteString(strings.ReplaceAll(line, search, replace))
out.WriteRune('\n')
})
}
// ReplaceRegexp filters its input by replacing all matches of the compiled
// regular expression `re` with the replacement string `replace`. Inside
// `replace`, $ signs are interpreted as in regexp.Expand, so for instance "$1"
// represents the text of the first submatch. If there is an error reading the
// pipe, the pipe's error status is also set.
func (p *Pipe) ReplaceRegexp(re *regexp.Regexp, replace string) *Pipe {
return p.EachLine(func(line string, out *strings.Builder) {
out.WriteString(re.ReplaceAllString(line, replace))
out.WriteRune('\n')
})
}