Skip to content

Commit a440dda

Browse files
committed
cli option for columns now counts first column as 1 rather than zero
1 parent 1b222ae commit a440dda

File tree

3 files changed

+34
-15
lines changed

3 files changed

+34
-15
lines changed

cmds/csvcols/csvcols.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ SYNOPSIS
4343
4444
%s converts a set of command line args into columns output in CSV format.
4545
It can also be used to filter input CSV and rendering only the column numbers
46-
listed on the commandline.
46+
listed on the commandline (first column is 1 not 0)
4747
`
4848

4949
examples = `
@@ -61,13 +61,13 @@ Example parsing a pipe delimited string into a CSV line
6161
%s -delimiter "|" "1|2|3" >> 3col.csv
6262
cat 3col.csv
6363
64-
Filter a 10 column CSV file for columns 0,3,5 (left most column is number zero)
64+
Filter a 10 column CSV file for columns 1,4,6 (left most column is number zero)
6565
66-
cat 10col.csv | csvcols -col 0 3 5 > 3col.csv
66+
cat 10col.csv | csvcols -col 1 4 6 > 3col.csv
6767
68-
Filter a 10 columns CSV file for columns 0,3,5 from input file
68+
Filter a 10 columns CSV file for columns 1,4,6 from input file
6969
70-
%s -i 10col.csv -col 0 3 5 > 3col.csv
70+
%s -i 10col.csv -col 1 4 6 > 3col.csv
7171
`
7272

7373
// Standard Options
@@ -187,8 +187,13 @@ func main() {
187187
columnNos := []int{}
188188
for _, arg := range args {
189189
i, err := strconv.Atoi(arg)
190+
// NOTE: We need to adjust from humans counting from 1 to counting from zero
191+
i--
192+
if i < 0 {
193+
i = 0
194+
}
190195
if err != nil {
191-
fmt.Fprintf(os.Stderr, "Expected a column number (0 - n), %q, %s\n", arg, err)
196+
fmt.Fprintf(os.Stderr, "Expected a column number in range of 1 to N, %q, %s\n", arg, err)
192197
os.Exit(1)
193198
}
194199
columnNos = append(columnNos, i)

cmds/csvfind/csvfind.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,21 @@ var (
3939
SYNOPSIS
4040
4141
%s processes a CSV file as input returning rows that contain the column
42-
with matched text. Supports exact match as well as some Levenshtein matching.
42+
with matched text. Columns are count from one instead of zero. Supports
43+
exact match as well as some Levenshtein matching.
4344
`
4445

4546
examples = `
4647
EXAMPLES
4748
4849
Find the rows where the third column matches "The Red Book of Westmarch" exactly
4950
50-
%s -i books.csv -col=1 "The Red Book of Westmarch"
51+
%s -i books.csv -col=2 "The Red Book of Westmarch"
5152
5253
Find the rows where the third column (colums numbered 0,1,2) matches approximately
5354
"The Red Book of Westmarch"
5455
55-
%s -i books.csv -col=1 -levenshtein \
56+
%s -i books.csv -col=2 -levenshtein \
5657
-insert-cost=1 -delete-cost=1 -substitute-cost=3 \
5758
-max-edit-distance=50 -append-edit-distance \
5859
"The Red Book of Westmarch"
@@ -61,7 +62,7 @@ In this example we've appended the edit distance to see how close the matches ar
6162
6263
You can also search for phrases in columns.
6364
64-
%s -i books.csv -col=1 -contains "Red Book"
65+
%s -i books.csv -col=2 -contains "Red Book"
6566
`
6667

6768
// Standard Options
@@ -141,10 +142,11 @@ func main() {
141142
os.Exit(0)
142143
}
143144

144-
if col < 0 {
145-
fmt.Fprintf(os.Stderr, "Cannot have a negative column reference %d\n", col)
145+
if col <= 0 {
146+
fmt.Fprintf(os.Stderr, "Cannot have a zero or negative column reference %d\n", col)
146147
os.Exit(1)
147148
}
149+
col = col - 1
148150

149151
args := flag.Args()
150152
if len(args) == 0 {

cmds/csvjoin/csvjoin.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ SYNOPSIS
4040
4141
%s outputs CSV content based on two CSV files with matching column values.
4242
Each CSV input file has a designated column to match on. The values are
43-
compared as strings.
43+
compared as strings. Columns are counted from one rather than zero.
4444
`
4545

4646
examples = `
@@ -51,8 +51,8 @@ and data2.csv where column 1 in data1.csv matches the value in
5151
column 3 of data2.csv with the results being written to
5252
merged-data.csv..
5353
54-
%s -csv1=data1.csv -col1=1 \
55-
-csv2=data2.csv -col2=3 \
54+
%s -csv1=data1.csv -col1=2 \
55+
-csv2=data2.csv -col2=4 \
5656
-output=merged-data.csv
5757
`
5858

@@ -196,6 +196,18 @@ func main() {
196196
os.Exit(0)
197197
}
198198

199+
// NOTE: We are counting columns for humans from 1 rather than zero.
200+
if col1 <= 0 {
201+
fmt.Fprintf(os.Stderr, "col1 must be one or greater, %d\n", col1)
202+
os.Exit(1)
203+
}
204+
if col2 <= 0 {
205+
fmt.Fprintf(os.Stderr, "col2 must be one or greater, %d\n", col2)
206+
os.Exit(1)
207+
}
208+
col1--
209+
col2--
210+
199211
// NOTE: we don't setup inputFName as we need at least two inputs to process the join.
200212
out, err := cli.Create(outputFName, os.Stdout)
201213
if err != nil {

0 commit comments

Comments
 (0)