-
-
Notifications
You must be signed in to change notification settings - Fork 85
/
replace.go
366 lines (320 loc) · 11.3 KB
/
replace.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
// Copyright © 2016-2021 Wei Shen <shenwei356@gmail.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package cmd
import (
"encoding/csv"
"fmt"
"regexp"
"runtime"
"strings"
"github.com/shenwei356/xopen"
"github.com/spf13/cobra"
)
// replaceCmd represents the replace command
var replaceCmd = &cobra.Command{
Use: "replace",
Short: "replace data of selected fields by regular expression",
Long: `replace data of selected fields by regular expression
Note that the replacement supports capture variables.
e.g. $1 represents the text of the first submatch.
ATTENTION: use SINGLE quote NOT double quotes in *nix OS.
Examples: Adding space to cell values.
csvtk replace -p "(.)" -r '$1 '
Or use the \ escape character.
csvtk replace -p "(.)" -r "\$1 "
more on: http://shenwei356.github.io/csvtk/usage/#replace
Special replacement symbols:
{nr} Record number, starting from 1
{kv} Corresponding value of the key (captured variable $n) by key-value file,
n can be specified by flag --key-capt-idx (default: 1)
`,
Run: func(cmd *cobra.Command, args []string) {
config := getConfigs(cmd)
files := getFileListFromArgsAndFile(cmd, args, true, "infile-list", true)
if len(files) > 1 {
checkError(fmt.Errorf("no more than one file should be given"))
}
runtime.GOMAXPROCS(config.NumCPUs)
pattern := getFlagString(cmd, "pattern")
replacement := getFlagString(cmd, "replacement")
ignoreCase := getFlagBool(cmd, "ignore-case")
if pattern == "" {
checkError(fmt.Errorf("flags -p (--pattern) needed"))
}
p := pattern
if ignoreCase {
p = "(?i)" + p
}
patternRegexp, err := regexp.Compile(p)
checkError(err)
kvFile := getFlagString(cmd, "kv-file")
keepKey := getFlagBool(cmd, "keep-key")
keyCaptIdx := getFlagPositiveInt(cmd, "key-capt-idx")
keyMissRepl := getFlagString(cmd, "key-miss-repl")
nrWidth := getFlagPositiveInt(cmd, "nr-width")
nrFormat := fmt.Sprintf("%%0%dd", nrWidth)
kvFileAllLeftColumnsAsValue := getFlagBool(cmd, "kv-file-all-left-columns-as-value")
var replaceWithNR bool
if reNR.MatchString(replacement) {
replaceWithNR = true
}
var replaceWithKV bool
var kvs map[string]string
if reKV.MatchString(replacement) {
replaceWithKV = true
if !regexp.MustCompile(`\(.+\)`).MatchString(pattern) {
checkError(fmt.Errorf(`value of -p (--pattern) must contains "(" and ")" to capture data which is used specify the KEY`))
}
if kvFile == "" {
checkError(fmt.Errorf(`since replacement symbol "{kv}"/"{KV}" found in value of flag -r (--replacement), tab-delimited key-value file should be given by flag -k (--kv-file)`))
}
log.Infof("read key-value file: %s", kvFile)
kvs, err = readKVs(kvFile, kvFileAllLeftColumnsAsValue)
if err != nil {
checkError(fmt.Errorf("read key-value file: %s", err))
}
if len(kvs) == 0 {
checkError(fmt.Errorf("no valid data in key-value file: %s", kvFile))
}
if ignoreCase {
kvs2 := make(map[string]string, len(kvs))
for k, v := range kvs {
kvs2[strings.ToLower(k)] = v
}
kvs = kvs2
}
log.Infof("%d pairs of key-value loaded", len(kvs))
}
fieldStr := getFlagString(cmd, "fields")
if fieldStr == "" {
checkError(fmt.Errorf("flag -f (--fields) needed"))
}
fields, colnames, negativeFields, needParseHeaderRow, _ := parseFields(cmd, fieldStr, ",", config.NoHeaderRow)
var fieldsMap map[int]struct{}
if len(fields) > 0 {
fields2 := make([]int, len(fields))
fieldsMap = make(map[int]struct{}, len(fields))
for i, f := range fields {
if negativeFields {
fieldsMap[f*-1] = struct{}{}
fields2[i] = f * -1
} else {
fieldsMap[f] = struct{}{}
fields2[i] = f
}
}
fields = fields2
}
fuzzyFields := getFlagBool(cmd, "fuzzy-fields")
outfh, err := xopen.Wopen(config.OutFile)
checkError(err)
defer outfh.Close()
writer := csv.NewWriter(outfh)
if config.OutTabs || config.Tabs {
if config.OutDelimiter == ',' {
writer.Comma = '\t'
} else {
writer.Comma = config.OutDelimiter
}
} else {
writer.Comma = config.OutDelimiter
}
for _, file := range files {
csvReader, err := newCSVReaderByConfig(config, file)
if err != nil {
if err == xopen.ErrNoContent {
log.Warningf("csvtk replace: skipping empty input file: %s", file)
continue
}
checkError(err)
}
csvReader.Run()
parseHeaderRow := needParseHeaderRow // parsing header row
parseHeaderRow2 := needParseHeaderRow
var colnames2fileds map[string][]int // column name -> []field
var colnamesMap map[string]*regexp.Regexp
checkFields := true
var record2 []string // for output
var r string
var found []string
var founds [][]string
var k string
nr := 0
printMetaLine := true
for chunk := range csvReader.Ch {
checkError(chunk.Err)
if printMetaLine && len(csvReader.MetaLine) > 0 {
outfh.WriteString(fmt.Sprintf("sep=%s\n", string(writer.Comma)))
printMetaLine = false
}
for _, record := range chunk.Data {
if parseHeaderRow { // parsing header row
colnames2fileds = make(map[string][]int, len(record))
for i, col := range record {
if _, ok := colnames2fileds[col]; !ok {
colnames2fileds[col] = []int{i + 1}
} else {
colnames2fileds[col] = append(colnames2fileds[col], i+1)
}
}
colnamesMap = make(map[string]*regexp.Regexp, len(colnames))
for _, col := range colnames {
if !fuzzyFields {
if negativeFields {
if _, ok := colnames2fileds[col[1:]]; !ok {
checkError(fmt.Errorf(`column "%s" not existed in file: %s`, col[1:], file))
}
} else {
if _, ok := colnames2fileds[col]; !ok {
checkError(fmt.Errorf(`column "%s" not existed in file: %s`, col, file))
}
}
}
if negativeFields {
colnamesMap[col[1:]] = fuzzyField2Regexp(col[1:])
} else {
colnamesMap[col] = fuzzyField2Regexp(col)
}
}
if len(fields) == 0 { // user gives the colnames
fields = []int{}
for _, col := range record {
var ok bool
if fuzzyFields {
for _, re := range colnamesMap {
if re.MatchString(col) {
ok = true
break
}
}
} else {
_, ok = colnamesMap[col]
}
if ok {
fields = append(fields, colnames2fileds[col]...)
}
}
}
fieldsMap = make(map[int]struct{}, len(fields))
for _, f := range fields {
fieldsMap[f] = struct{}{}
}
parseHeaderRow = false
}
if checkFields {
for field := range fieldsMap {
if field > len(record) {
checkError(fmt.Errorf(`field (%d) out of range (%d) in file: %s`, field, len(record), file))
}
}
fields2 := []int{}
for f := range record {
_, ok := fieldsMap[f+1]
if negativeFields {
if !ok {
fields2 = append(fields2, f+1)
}
} else {
if ok {
fields2 = append(fields2, f+1)
}
}
}
fields = fields2
if len(fields) == 0 {
checkError(fmt.Errorf("no fields matched in file: %s", file))
}
fieldsMap = make(map[int]struct{}, len(fields))
for _, f := range fields {
fieldsMap[f] = struct{}{}
}
record2 = make([]string, len(record))
checkFields = false
}
if parseHeaderRow2 { // do not replace head line
checkError(writer.Write(record))
parseHeaderRow2 = false
continue
}
nr++
for f := range record {
record2[f] = record[f]
if _, ok := fieldsMap[f+1]; ok {
r = replacement
if replaceWithNR {
r = reNR.ReplaceAllString(r, fmt.Sprintf(nrFormat, nr))
}
if replaceWithKV {
founds = patternRegexp.FindAllStringSubmatch(record2[f], -1)
if len(founds) > 1 {
checkError(fmt.Errorf(`pattern "%s" matches multiple targets in "%s", this will cause chaos`, p, record2[f]))
}
if len(founds) > 0 {
found = founds[0]
if keyCaptIdx > len(found)-1 {
checkError(fmt.Errorf("value of flag -I (--key-capt-idx) overflows"))
}
k = string(found[keyCaptIdx])
if ignoreCase {
k = strings.ToLower(k)
}
if _, ok = kvs[k]; ok {
r = reKV.ReplaceAllString(r, kvs[k])
} else if keepKey {
r = reKV.ReplaceAllString(r, found[keyCaptIdx])
} else {
r = reKV.ReplaceAllString(r, keyMissRepl)
}
}
}
record2[f] = patternRegexp.ReplaceAllString(record2[f], r)
}
}
checkError(writer.Write(record2))
}
}
readerReport(&config, csvReader, file)
}
writer.Flush()
checkError(writer.Error())
},
}
func init() {
RootCmd.AddCommand(replaceCmd)
replaceCmd.Flags().StringP("fields", "f", "1", `select only these fields. e.g -f 1,2 or -f columnA,columnB`)
replaceCmd.Flags().BoolP("fuzzy-fields", "F", false, `using fuzzy fields, e.g., -F -f "*name" or -F -f "id123*"`)
replaceCmd.Flags().StringP("pattern", "p", "", "search regular expression")
replaceCmd.Flags().StringP("replacement", "r", "",
"replacement. supporting capture variables. "+
" e.g. $1 represents the text of the first submatch. "+
"ATTENTION: for *nix OS, use SINGLE quote NOT double quotes or "+
`use the \ escape character. Record number is also supported by "{nr}".`+
`use ${1} instead of $1 when {kv} given!`)
replaceCmd.Flags().BoolP("ignore-case", "i", false, "ignore case")
replaceCmd.Flags().StringP("kv-file", "k", "",
`tab-delimited key-value file for replacing key with value when using "{kv}" in -r (--replacement)`)
replaceCmd.Flags().BoolP("keep-key", "K", false, "keep the key as value when no value found for the key")
replaceCmd.Flags().IntP("key-capt-idx", "", 1, "capture variable index of key (1-based)")
replaceCmd.Flags().StringP("key-miss-repl", "", "", "replacement for key with no corresponding value")
replaceCmd.Flags().IntP("nr-width", "", 1, `minimum width for {nr} in flag -r/--replacement. e.g., formating "1" to "001" by --nr-width 3`)
replaceCmd.Flags().BoolP("kv-file-all-left-columns-as-value", "A", false, "treat all columns except 1th one as value for kv-file with more than 2 columns")
}
var reNR = regexp.MustCompile(`\{(NR|nr)\}`)
var reKV = regexp.MustCompile(`\{(KV|kv)\}`)