Skip to content

Commit

Permalink
fix specifying the position for the new column containing only a cons…
Browse files Browse the repository at this point in the history
…tant string. #252
  • Loading branch information
shenwei356 committed Nov 17, 2023
1 parent 95f9c5a commit a7ff18b
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 57 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
- support column name containing colons. [#254](https://github.com/shenwei356/csvtk/issues/254)
- `csvtk filter2`:
- update doc: add the `in` keyword. [#195](https://github.com/shenwei356/csvtk/pull/195)
- fix specifying the position for the new column containing only a constant string. [#252](https://github.com/shenwei356/csvtk/issues/252)
- `csvtk plot`:
- add a new flag `--tick-label-size`.
- `csvtk del-header`:
Expand Down
59 changes: 2 additions & 57 deletions csvtk/cmd/mutate2.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,62 +171,6 @@ Custom functions:
}
}

// expressions doe not contains `$`
if !reFilter2.MatchString(exprStr) {
// checkError(fmt.Errorf("invalid expression: %s", exprStr))
var expression *govaluate.EvaluableExpression
if containCustomFuncs {
expression, err = govaluate.NewEvaluableExpressionWithFunctions(exprStr, functions)
} else {
expression, err = govaluate.NewEvaluableExpression(exprStr)
}
checkError(err)
var result interface{}
result, err = expression.Evaluate(emptyParams)
if err != nil {
checkError(fmt.Errorf("fail to evaluate: %s", exprStr))
}
result2 := fmt.Sprintf("%v", result)
for _, file := range files {
var csvReader *CSVReader
csvReader, err = newCSVReaderByConfig(config, file)

if err != nil {
if err == xopen.ErrNoContent {
log.Warningf("csvtk mutate2: skipping empty input file: %s", file)
continue
}
checkError(err)
}

csvReader.Read(ReadOption{
FieldStr: "1-",
})

checkFirstLine := true
for record := range csvReader.Ch {
if record.Err != nil {
checkError(record.Err)
}

if checkFirstLine {
checkFirstLine = false

if !config.NoHeaderRow || record.IsHeaderRow { // do not replace head line
checkError(writer.Write(append(record.All, name)))
continue
}
}

checkError(writer.Write(append(record.All, result2)))
}

readerReport(&config, csvReader, file)
}

return
}

decimalWidth := getFlagNonNegativeInt(cmd, "decimal-width")
decimalFormat := fmt.Sprintf("%%.%df", decimalWidth)

Expand Down Expand Up @@ -330,6 +274,7 @@ Custom functions:
value = name
record2 = record.All
record2 = append(record2, value)

if after != "" {
if _fields, ok = colnames2fileds[after]; ok {
at = _fields[len(_fields)-1] + 1
Expand Down Expand Up @@ -475,8 +420,8 @@ Custom functions:
}

record2 = record.All

record2 = append(record2, value)

if after != "" {
if _fields, ok = colnames2fileds[after]; ok {
at = _fields[len(_fields)-1] + 1
Expand Down

0 comments on commit a7ff18b

Please sign in to comment.