Skip to content

Commit d404dd0

Browse files
committed
fixed bug: csv-find -trimspace issue #2
1 parent a957aee commit d404dd0

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

cmds/csvfind/csvfind.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import (
3333
var (
3434
description = `
3535
%s processes a CSV file as input returning rows that contain the column
36-
with matched text. Columns are count from one instead of zero. Supports
36+
with matched text. Columns are counted from one instead of zero. Supports
3737
exact match as well as some Levenshtein matching.
3838
`
3939

@@ -42,7 +42,7 @@ Find the rows where the third column matches "The Red Book of Westmarch" exactly
4242
4343
%s -i books.csv -col=2 "The Red Book of Westmarch"
4444
45-
Find the rows where the third column (colums numbered 0,1,2) matches approximately
45+
Find the rows where the third column (colums numbered 1,2,3) matches approximately
4646
"The Red Book of Westmarch"
4747
4848
%s -i books.csv -col=2 -levenshtein \
@@ -123,7 +123,7 @@ func main() {
123123
app.StringVar(&stopWordsOption, "stop-words", "", "use the colon delimited list of stop words")
124124
app.BoolVar(&skipHeaderRow, "skip-header-row", true, "skip the header row")
125125
app.BoolVar(&allowDuplicates, "allow-duplicates", true, "allow duplicates when searching for matches")
126-
app.BoolVar(&trimSpaces, "trimspaces", false, "trim spaces around cell values before comparing")
126+
app.BoolVar(&trimSpaces, "trimspace,trimspaces", false, "trim spaces around cell values before comparing")
127127

128128
// Parse env and options
129129
app.Parse()
@@ -214,6 +214,9 @@ func main() {
214214
if caseSensitive == false {
215215
src = strings.ToLower(src)
216216
}
217+
if trimSpaces == true {
218+
src = strings.TrimSpace(src)
219+
}
217220
if len(stopWords) > 0 {
218221
// Split into fields applying datatools filter
219222
fields := strings.FieldsFunc(src, func(c rune) bool {

how-to/csvfind/trimspace.csv

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
name,location,information
2+
red,library,color
3+
blue, field,classification
4+
yellow ,house,paint

test_cmds.bash

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,24 @@ function test_csvfind() {
437437
R=$(cmp how-to/csvfind/result3.csv temp.csv)
438438
assert_empty "test_csvfind (contains)" "$R"
439439

440+
# csvfind -trimspaces issue #2
441+
E="red,library,color"
442+
R=$(bin/csvfind -nl=false -i how-to/csvfind/trimspace.csv -col=2 -trimspaces "library")
443+
assert_equal "test_csvfind (trimspaces library)" "$E" "$R"
444+
445+
E="red,library,color"
446+
R=$(bin/csvfind -nl=false -i how-to/csvfind/trimspace.csv -col=1 -trimspaces "red")
447+
assert_equal "test_csvfind (trimspaces red)" "$E" "$R"
448+
449+
E='blue," field",classification'
450+
R=$(bin/csvfind -nl=false -i how-to/csvfind/trimspace.csv -col=2 -trimspaces "field")
451+
assert_equal "test_csvfind (trimspaces field)" "$E" "$R"
452+
453+
E='" yellow ",house,paint'
454+
R=$(bin/csvfind -nl=false -i how-to/csvfind/trimspace.csv -col=1 -trimspaces "yellow")
455+
assert_equal "test_csvfind (trimspaces yellow)" "$E" "$R"
456+
457+
440458
if [ -f temp.csv ]; then rm temp.csv; fi
441459
echo "test_csvfind OK";
442460
}

0 commit comments

Comments
 (0)