Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Join option to add filename as prefix to header #203

Merged
merged 2 commits into from
Oct 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions csvtk/cmd/join.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"fmt"
"runtime"
"strings"
"path/filepath"

"github.com/shenwei356/xopen"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -65,6 +66,7 @@ Attention:
}

ignoreCase := getFlagBool(cmd, "ignore-case")
filenameAsPrefix := getFlagBool(cmd, "prefix-filename")

fuzzyFields := getFlagBool(cmd, "fuzzy-fields")
leftJoin := getFlagBool(cmd, "left-join")
Expand Down Expand Up @@ -105,6 +107,8 @@ Attention:
}

var HeaderRow []string
var newColname string
var prefixedHeaderRow []string
var Data [][]string
var Fields []int
firstFile := true
Expand Down Expand Up @@ -153,6 +157,13 @@ Attention:
}
if firstFile {
HeaderRow, Data, Fields = headerRow, data, fields
if filenameAsPrefix {
var Colname string
for f, Colname = range headerRow {
newColname = fmt.Sprintf("%s-%s", filepath.Base(file), Colname)
prefixedHeaderRow = append(prefixedHeaderRow, newColname)
}
}
firstFile = false
if len(HeaderRow) > 0 {
withHeaderRow = true
Expand Down Expand Up @@ -235,6 +246,8 @@ Attention:
for f, colname = range headerRow {
if _, ok = fieldsMap[f+1]; !ok {
newHeaderRow = append(newHeaderRow, colname)
newColname = fmt.Sprintf("%s-%s", filepath.Base(file), colname)
prefixedHeaderRow = append(prefixedHeaderRow, newColname)
}
}
HeaderRow = newHeaderRow
Expand Down Expand Up @@ -276,10 +289,14 @@ Attention:
}
}
Data = Data2
}
}

if withHeaderRow {
checkError(writer.Write(HeaderRow))
if filenameAsPrefix {
checkError(writer.Write(prefixedHeaderRow))
} else {
checkError(writer.Write(HeaderRow))
}
}
for _, record := range Data {
checkError(writer.Write(record))
Expand All @@ -302,4 +319,5 @@ func init() {
joinCmd.Flags().BoolP("outer-join", "O", false, `outer join, exclusive with --left-join`)
joinCmd.Flags().StringP("na", "", "", "content for filling NA data")
joinCmd.Flags().BoolP("ignore-null", "n", false, "do not match NULL values")
joinCmd.Flags().BoolP("prefix-filename", "A", false, "add each filename as a prefix to header")
}
1 change: 1 addition & 0 deletions doc/docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -1887,6 +1887,7 @@ Flags:
-L, --left-join left join, equals to -k/--keep-unmatched, exclusive with --outer-join
--na string content for filling NA data
-O, --outer-join outer join, exclusive with --left-join
-A, --prefix-filename add each filename as a prefix to header

```

Expand Down