Skip to content

Commit

Permalink
fix bug of mutate for unmatched data
Browse files Browse the repository at this point in the history
  • Loading branch information
shenwei356 committed Apr 10, 2016
1 parent 7c30711 commit b223b6a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
14 changes: 12 additions & 2 deletions csvtk/cmd/mutate.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ var mutateCmd = &cobra.Command{
checkError(fmt.Errorf("falg -n (--name) needed"))
}
ignoreCase := getFlagBool(cmd, "ignore-case")
naUnmatched := getFlagBool(cmd, "na")
pattern := getFlagString(cmd, "pattern")
if !regexp.MustCompile(`\(.+\)`).MatchString(pattern) {
checkError(fmt.Errorf(`value of -p (--pattern) must contains "(" and ")" to capture data which is used to create new column`))
Expand Down Expand Up @@ -193,8 +194,16 @@ var mutateCmd = &cobra.Command{
record2 = append(record2, name)
handleHeaderRow = false
} else {
found := patternRegexp.FindAllStringSubmatch(record[f], -1)
record2 = append(record2, found[0][0])
if patternRegexp.MatchString(record[f]) {
found := patternRegexp.FindAllStringSubmatch(record[f], -1)
record2 = append(record2, found[0][0])
} else {
if naUnmatched {
record2 = append(record2, "")
} else {
record2 = append(record2, record[f])
}
}
}
break
}
Expand All @@ -214,4 +223,5 @@ func init() {
mutateCmd.Flags().StringP("pattern", "p", "^(.+)$", `search regular expression with capture bracket. e.g.`)
mutateCmd.Flags().StringP("name", "n", "", `new column name`)
mutateCmd.Flags().BoolP("ignore-case", "i", false, "ignore case")
mutateCmd.Flags().BoolP("na", "", false, "for unmatched data, use blank instead of orginal data")
}
2 changes: 1 addition & 1 deletion csvtk/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var RootCmd = &cobra.Command{
Short: "Another cross-platform, efficient and practical CSV/TSV tool kit",
Long: `Another cross-platform, efficient and practical CSV/TSV tool kit
Version: 0.2
Version: 0.2.1
Author: Wei Shen <[email protected]>
Expand Down

0 comments on commit b223b6a

Please sign in to comment.