Skip to content

Commit

Permalink
fix csvtk cut -H
Browse files Browse the repository at this point in the history
  • Loading branch information
shenwei356 committed Oct 30, 2020
1 parent 1fdd424 commit d5bd424
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
33 changes: 31 additions & 2 deletions csvtk/cmd/cut.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ var cutCmd = &cobra.Command{
var colnames2fileds map[string]int // column name -> field
var colnamesMap map[string]*regexp.Regexp

checkFields := true
var items []string
var noRecord bool

Expand Down Expand Up @@ -137,6 +138,7 @@ var cutCmd = &cobra.Command{
}
}

// matching colnames
var ok bool
if negativeFields {
for _, col := range record {
Expand Down Expand Up @@ -185,8 +187,6 @@ var cutCmd = &cobra.Command{
}
}

// matching colnames

} else {
for _, f := range fields {
if f > len(record) {
Expand All @@ -213,9 +213,38 @@ var cutCmd = &cobra.Command{

items = make([]string, len(fields))

checkFields = false
parseHeaderRow = false
}

if checkFields {
for _, f := range fields {
if f > len(record) {
checkError(fmt.Errorf(`field (%d) out of range (%d) in file: %s`, f, len(record), file))
}
}

if negativeFields {
fields2 := make([]int, 0, len(fields))
var ok bool
for i := range record {
if _, ok = fieldsMap[i+1]; !ok {
fields2 = append(fields2, i+1)
}
}
fields = fields2
}

if len(fields) == 0 {
noRecord = true
break
}

items = make([]string, len(fields))

checkFields = false
}

for i, f := range fields {
items[i] = record[f-1]
}
Expand Down
2 changes: 1 addition & 1 deletion doc/docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -1084,7 +1084,7 @@ Flags:
-F, --fuzzy-fields using fuzzy fields, e.g., -F -f "*name" or -F -f "id123*"
-h, --help help for cut
-i, --ignore-case ignore case (column name)
-u, --uniq-column deduplicate columns when given fuzzy column names
-u, --uniq-column deduplicate columns matched by multiple fuzzy column names
```

Expand Down

0 comments on commit d5bd424

Please sign in to comment.