Skip to content

Commit c7a9cde

Browse files
committed
added -use-lazy-quotes and -trim-leading-space to all csv commands for CSV input parsing
1 parent 53ae7cf commit c7a9cde

File tree

12 files changed

+173
-76
lines changed

12 files changed

+173
-76
lines changed

cmds/csv2json/csv2json.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,11 @@ Convert data1.csv to JSON blobs, one line per blob
6565
eol string
6666

6767
// Application Options
68-
useHeader bool
69-
asBlobs bool
70-
delimiter string
68+
useHeader bool
69+
asBlobs bool
70+
delimiter string
71+
lazyQuotes bool
72+
trimLeadingSpace bool
7173
)
7274

7375
func main() {
@@ -94,6 +96,8 @@ func main() {
9496
app.BoolVar(&useHeader, "use-header", true, "treat the first row as field names")
9597
app.BoolVar(&asBlobs, "as-blobs", false, "output as one JSON blob per line")
9698
app.StringVar(&delimiter, "d,delimiter", "", "set the delimter character")
99+
app.BoolVar(&lazyQuotes, "use-lazy-quotes", false, "use lazy quotes for for CSV input")
100+
app.BoolVar(&trimLeadingSpace, "trim-leading-space", false, "trim leading space in fields for CSV input")
97101

98102
// Parse environment and options
99103
app.Parse()
@@ -140,6 +144,8 @@ func main() {
140144
rowNo := 0
141145
fieldNames := []string{}
142146
r := csv.NewReader(app.In)
147+
r.LazyQuotes = lazyQuotes
148+
r.TrimLeadingSpace = trimLeadingSpace
143149
if delimiter != "" {
144150
r.Comma = datatools.NormalizeDelimiterRune(delimiter)
145151
}

cmds/csv2mdtable/csv2mdtable.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ Convert data1.csv to data1.md using options.
6060
eol string
6161

6262
// Application Options
63-
delimiter string
63+
delimiter string
64+
lazyQuotes bool
65+
trimLeadingSpace bool
6466
)
6567

6668
func main() {
@@ -85,6 +87,8 @@ func main() {
8587

8688
// Application Options
8789
app.StringVar(&delimiter, "d,delimiter", "", "set delimiter character")
90+
app.BoolVar(&lazyQuotes, "use-lazy-quotes", false, "using lazy quotes for CSV input")
91+
app.BoolVar(&trimLeadingSpace, "trim-leading-space", false, "trim leading space in field(s) for CSV input")
8892

8993
// Parse environment and options
9094
app.Parse()
@@ -129,6 +133,9 @@ func main() {
129133
}
130134

131135
r := csv.NewReader(app.In)
136+
r.LazyQuotes = lazyQuotes
137+
r.TrimLeadingSpace = trimLeadingSpace
138+
132139
if delimiter != "" {
133140
r.Comma = datatools.NormalizeDelimiterRune(delimiter)
134141
}

cmds/csv2xlsx/csv2xlsx.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,14 @@ the workbook's 'My worksheet 2' sheet.
6565
quiet bool
6666

6767
// App Specific Options
68-
workbookName string
69-
sheetName string
70-
delimiter string
68+
workbookName string
69+
sheetName string
70+
delimiter string
71+
lazyQuotes bool
72+
trimLeadingSpace bool
7173
)
7274

73-
func csv2XLSXSheet(in *os.File, workbookName string, sheetName string, delimiter string) error {
75+
func csv2XLSXSheet(in *os.File, workbookName string, sheetName string, delimiter string, lazyQuotes, trimLeadingSpace bool) error {
7476
var workbook *xlsx.File
7577

7678
// Open the workbook
@@ -89,6 +91,9 @@ func csv2XLSXSheet(in *os.File, workbookName string, sheetName string, delimiter
8991
}
9092

9193
r := csv.NewReader(in)
94+
r.LazyQuotes = lazyQuotes
95+
r.TrimLeadingSpace = trimLeadingSpace
96+
9297
if delimiter != "" {
9398
r.Comma = datatools.NormalizeDelimiterRune(delimiter)
9499
}
@@ -134,6 +139,8 @@ func main() {
134139
app.StringVar(&workbookName, "workbook", "", "Workbook name")
135140
app.StringVar(&sheetName, "sheet", "", "Sheet name to create/replace")
136141
app.StringVar(&delimiter, "d,delimiter", "", "set delimiter character (input)")
142+
app.BoolVar(&lazyQuotes, "use-lazy-quotes", false, "use lazy quotes for CSV input")
143+
app.BoolVar(&trimLeadingSpace, "trim-leading-space", false, "trim leading space in field(s) for CSV input")
137144

138145
// Parse environment and options
139146
app.Parse()
@@ -196,6 +203,6 @@ func main() {
196203
if len(sheetName) == 0 {
197204
cli.ExitOnError(app.Eout, fmt.Errorf("Missing sheet name"), quiet)
198205
}
199-
err = csv2XLSXSheet(app.In, workbookName, sheetName, delimiter)
206+
err = csv2XLSXSheet(app.In, workbookName, sheetName, delimiter, lazyQuotes, trimLeadingSpace)
200207
cli.ExitOnError(app.Eout, err, quiet)
201208
}

cmds/csvcleaner/csvcleaner.go

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,15 @@ Normalizing a spread sheet's column count to 5 padding columns as needed per row
4848
4949
cat mysheet.csv | %s -field-per-row=5
5050
51-
Trim leading spaces.
51+
Trim leading spaces from output.
5252
5353
cat mysheet.csv | %s -left-trim
5454
55-
Trim trailing spaces.
55+
Trim trailing spaces from output.
5656
5757
cat mysheet.csv | %s -right-trim
5858
59-
Trim leading and trailing spaces
59+
Trim leading and trailing spaces from output.
6060
6161
cat mysheet.csv | %s -trim
6262
`
@@ -74,18 +74,19 @@ Trim leading and trailing spaces
7474
//eol string
7575

7676
// App Options
77-
comma string
78-
rowComment string
79-
fieldsPerRecord int
80-
lazyQuotes bool
81-
trailingComma bool
82-
trimSpace bool
83-
trimLeadingSpace bool
84-
trimTrailingSpace bool
85-
reuseRecord bool
86-
commaOut string
87-
useCRLF bool
88-
stopOnError bool
77+
comma string
78+
rowComment string
79+
fieldsPerRecord int
80+
trailingComma bool
81+
trimSpace bool
82+
trimLeftSpace bool
83+
trimRightSpace bool
84+
reuseRecord bool
85+
commaOut string
86+
useCRLF bool
87+
stopOnError bool
88+
lazyQuotes bool
89+
trimLeadingSpace bool
8990

9091
verbose bool
9192
)
@@ -112,16 +113,17 @@ func main() {
112113

113114
// Application specific options
114115
app.IntVar(&fieldsPerRecord, "fields-per-row", 0, "set the number of columns to output right padding empty cells as needed")
115-
app.BoolVar(&lazyQuotes, "use-lazy-quoting", false, "If LazyQuotes is true, a quote may appear in an unquoted field and a non-doubled quote may appear in a quoted field.")
116-
app.BoolVar(&trimSpace, "trim", false, "If set to true leading and trailing white space in a field is ignored.")
117-
app.BoolVar(&trimLeadingSpace, "left-trim", false, "If set to true leading white space in a field is ignored.")
118-
app.BoolVar(&trimTrailingSpace, "right-trim", false, "If set to true trailing white space in a field is ignored.")
116+
app.BoolVar(&trimSpace, "trim", false, "trim spaces on CSV out")
117+
app.BoolVar(&trimLeftSpace, "left-trim", false, "left trim spaces on CSV out")
118+
app.BoolVar(&trimRightSpace, "right-trim", false, "right trim spaces on CSV out")
119119
app.BoolVar(&reuseRecord, "reuse", true, "if false then a new array is allocated for each row processed, if true the array gets reused")
120120
app.StringVar(&comma, "comma", "", "if set use this character in place of a comma for delimiting cells")
121121
app.StringVar(&rowComment, "comment-char", "", "if set, rows starting with this character will be ignored as comments")
122122
app.StringVar(&commaOut, "output-comma", "", "if set use this character in place of a comma for delimiting output cells")
123123
app.BoolVar(&useCRLF, "use-crlf", false, "if set use a charage return and line feed in output")
124124
app.BoolVar(&stopOnError, "stop-on-error", false, "exit on error, useful if you're trying to debug a problematic CSV file")
125+
app.BoolVar(&lazyQuotes, "use-lazy-quotes", false, "use lazy quotes for CSV input")
126+
app.BoolVar(&trimLeadingSpace, "trim-leading-space", false, "trim leading space from field(s) for CSV input")
125127

126128
app.BoolVar(&verbose, "V,verbose", false, "write verbose output to standard error")
127129

@@ -162,16 +164,10 @@ func main() {
162164
fmt.Fprintln(app.Out, app.Version())
163165
os.Exit(0)
164166
}
165-
/*
166-
if newLine {
167-
eol = "\n"
168-
}
169-
*/
170167

171168
// Loop through input CSV, apply options, write to output CSV
172-
if trimSpace == true {
173-
trimLeadingSpace = true
174-
trimTrailingSpace = true
169+
if trimLeftSpace == true && trimRightSpace == true {
170+
trimSpace = true
175171
}
176172

177173
// Setup our CSV reader with any cli options
@@ -237,10 +233,17 @@ func main() {
237233
}
238234
}
239235
}
240-
if trimSpace {
236+
if trimSpace || trimLeftSpace || trimRightSpace {
241237
for i := range row {
242238
s := row[i]
243-
row[i] = strings.TrimSpace(s)
239+
switch {
240+
case trimSpace:
241+
row[i] = strings.TrimSpace(s)
242+
case trimRightSpace:
243+
row[i] = strings.TrimRight(s, " \t\n\r")
244+
case trimLeftSpace:
245+
row[i] = strings.TrimLeft(s, " \t\n\r")
246+
}
244247
}
245248
}
246249
if err := w.Write(row); err != nil {

cmds/csvcols/csvcols.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,13 @@ Using options filter a 3 column CSV file for columns 1,3 into 2col.csv
7474
quiet bool
7575

7676
// App Options
77-
outputColumns string
78-
prefixUUID bool
79-
skipHeaderRow bool
80-
delimiter string
81-
outputDelimiter string
77+
outputColumns string
78+
prefixUUID bool
79+
skipHeaderRow bool
80+
delimiter string
81+
outputDelimiter string
82+
lazyQuotes bool
83+
trimLeadingSpace bool
8284
)
8385

8486
func selectedColumns(rowNo int, record []string, columnNos []int, prefixUUID bool, skipHeaderRow bool) []string {
@@ -106,10 +108,13 @@ func selectedColumns(rowNo int, record []string, columnNos []int, prefixUUID boo
106108
return result
107109
}
108110

109-
func CSVColumns(in *os.File, out *os.File, columnNos []int, prefixUUID bool, skipHeaderRow bool, delimiterIn string, delimiterOut string) {
111+
func CSVColumns(in *os.File, out *os.File, columnNos []int, prefixUUID bool, skipHeaderRow bool, delimiterIn string, delimiterOut string, lazyQuotes, trimLeadingSpace bool) {
110112
var err error
111113

112114
r := csv.NewReader(in)
115+
r.LazyQuotes = lazyQuotes
116+
r.TrimLeadingSpace = trimLeadingSpace
117+
113118
w := csv.NewWriter(out)
114119
if delimiterIn != "" {
115120
r.Comma = datatools.NormalizeDelimiterRune(delimiterIn)
@@ -161,6 +166,8 @@ func main() {
161166
app.StringVar(&outputDelimiter, "od,output-delimiter", "", "set the output delimiter character")
162167
app.BoolVar(&skipHeaderRow, "skip-header-row", true, "skip the header row")
163168
app.BoolVar(&prefixUUID, "uuid", false, "add a prefix row with generated UUID cell")
169+
app.BoolVar(&lazyQuotes, "use-lazy-quotes", false, "use lazy quotes on CSV input")
170+
app.BoolVar(&trimLeadingSpace, "trim-leading-space", false, "trim leading space in field(s) for CSV input")
164171

165172
// Parse env and options
166173
app.Parse()
@@ -212,7 +219,7 @@ func main() {
212219
columnNos[i] = 0
213220
}
214221
}
215-
CSVColumns(app.In, app.Out, columnNos, prefixUUID, skipHeaderRow, delimiter, outputDelimiter)
222+
CSVColumns(app.In, app.Out, columnNos, prefixUUID, skipHeaderRow, delimiter, outputDelimiter, lazyQuotes, trimLeadingSpace)
216223
os.Exit(0)
217224
}
218225

cmds/csvfind/csvfind.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ You can also search for phrases in columns.
8484
trimSpaces bool
8585
allowDuplicates bool
8686
delimiter string
87+
lazyQuotes bool
88+
trimLeadingSpace bool
8789
)
8890

8991
func main() {
@@ -124,6 +126,8 @@ func main() {
124126
app.BoolVar(&skipHeaderRow, "skip-header-row", true, "skip the header row")
125127
app.BoolVar(&allowDuplicates, "allow-duplicates", true, "allow duplicates when searching for matches")
126128
app.BoolVar(&trimSpaces, "trimspace,trimspaces", false, "trim spaces around cell values before comparing")
129+
app.BoolVar(&lazyQuotes, "use-lazy-quotes", false, "use lazy quotes on CSV input")
130+
app.BoolVar(&trimLeadingSpace, "trim-leading-space", false, "trim leadings space in field(s) for CSV input")
127131

128132
// Parse env and options
129133
app.Parse()
@@ -190,6 +194,8 @@ func main() {
190194
}
191195

192196
csvIn := csv.NewReader(app.In)
197+
csvIn.LazyQuotes = lazyQuotes
198+
csvIn.TrimLeadingSpace = trimLeadingSpace
193199
csvOut := csv.NewWriter(app.Out)
194200
if delimiter != "" {
195201
csvIn.Comma = datatools.NormalizeDelimiterRune(delimiter)

cmds/csvjoin/csvjoin.go

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -58,23 +58,25 @@ merged-data.csv..
5858
quiet bool
5959

6060
// App Options
61-
verbose bool
62-
csv1FName string
63-
csv2FName string
64-
col1 int
65-
col2 int
66-
trimSpaces bool
67-
caseSensitive bool
68-
useContains bool
69-
useLevenshtein bool
70-
insertCost int
71-
deleteCost int
72-
substituteCost int
73-
maxEditDistance int
74-
stopWordsOption string
75-
allowDuplicates bool
76-
asInMemory bool
77-
delimiter string
61+
verbose bool
62+
csv1FName string
63+
csv2FName string
64+
col1 int
65+
col2 int
66+
trimSpaces bool
67+
caseSensitive bool
68+
useContains bool
69+
useLevenshtein bool
70+
insertCost int
71+
deleteCost int
72+
substituteCost int
73+
maxEditDistance int
74+
stopWordsOption string
75+
allowDuplicates bool
76+
asInMemory bool
77+
delimiter string
78+
lazyQuotes bool
79+
trimLeadingSpace bool
7880
)
7981

8082
// cellsMatch checks if two cells' values match
@@ -185,6 +187,8 @@ func main() {
185187
app.BoolVar(&trimSpaces, "trimspaces", false, "trim spaces around cell values before comparing")
186188
app.BoolVar(&asInMemory, "in-memory", false, "if true read both CSV files")
187189
app.StringVar(&delimiter, "d,delimiter", "", "set delimiter character")
190+
app.BoolVar(&lazyQuotes, "use-lazy-quotes", false, "use lazy quotes for CSV input")
191+
app.BoolVar(&trimLeadingSpace, "trim-leading-space", false, "trim leading space in field(s) for CSV input")
188192

189193
// Parse env and options
190194
app.Parse()
@@ -261,11 +265,15 @@ func main() {
261265
cli.ExitOnError(app.Eout, err, quiet)
262266
defer fp1.Close()
263267
csv1 := csv.NewReader(fp1)
268+
csv1.LazyQuotes = lazyQuotes
269+
csv1.TrimLeadingSpace = trimLeadingSpace
264270

265271
fp2, err := os.Open(csv2FName)
266272
cli.ExitOnError(app.Eout, err, quiet)
267273
defer fp2.Close()
268274
csv2 := csv.NewReader(fp2)
275+
csv2.LazyQuotes = lazyQuotes
276+
csv2.TrimLeadingSpace = trimLeadingSpace
269277

270278
w := csv.NewWriter(app.Out)
271279
if delimiter != "" {

0 commit comments

Comments
 (0)