Skip to content

Commit

Permalink
add a new global flag -U, --no-out-header for disable outputing the h…
Browse files Browse the repository at this point in the history
…eader row. #258 #264
  • Loading branch information
shenwei356 committed Mar 13, 2024
1 parent 0a5f0c6 commit 0eb045e
Show file tree
Hide file tree
Showing 29 changed files with 143 additions and 32 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
- `csvtk`:
- grouping subcommands in help message.
- add a new global flag `--quiet`. [#261](https://github.com/shenwei356/csvtk/issues/261)
- add a new global flag `-U, --delete-header` for disable outputing the header row. Supported commands: concat, csv2tab/tab2csv, csv2xlsx/xlsx2csv, cut, filter, filter2, freq, fold/unfold, gather, fmtdate, grep, head, join, mutate, mutate2, replace, round, sample. [#258](https://github.com/shenwei356/csvtk/issues/258)
- support more commands with `-Z/--show-row-number`: head.
- `csvtk dim`:
- fix duplicated rows for multiple input files, this bug was introduced in v0.27.0.
- `csvtk concat`:
Expand All @@ -11,6 +13,8 @@
- fix flag checking of `-k` and `-v`.
- `csvtk sort`:
- fix ordering when given multiple custom levels.
- `csvtk filter/filter2`:
- fix printing row number with `-Z`.
- `csvtk xls2csv`:
- output raw data. [#262](https://github.com/shenwei356/csvtk/issues/262)
- `csvtk pretty`:
Expand Down
2 changes: 1 addition & 1 deletion csvtk/cmd/concat.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ so only columns match that of the first files kept.
checkError(writer.Error())
}()

if !config.NoHeaderRow {
if !config.NoHeaderRow && !config.NoOutHeader {
colnames := make([]string, len(COLNAMES))
for i, col := range COLNAMES {
colnames[i] = COLNAME2OLDNAME[col]
Expand Down
2 changes: 1 addition & 1 deletion csvtk/cmd/csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ func (csvReader *CSVReader) Read(opt ReadOption) {
Row: row,
All: record, // copied values
Fields: fields, // the first variable
Selected: items, // copied values
Selected: items, // copied values. Warning: if showRowNumber is true, the first vlaue is the row number

IsHeaderRow: isHeaderRow,
SelectWithColnames: selectWithColnames,
Expand Down
9 changes: 9 additions & 0 deletions csvtk/cmd/csv2tab.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ var csv2tabCmd = &cobra.Command{
writer.Comma = '\t'

for _, file := range files {
handleHeaderRow := !config.NoHeaderRow

csvReader, err := newCSVReaderByConfig(config, file)
if err != nil {
if err == xopen.ErrNoContent {
Expand All @@ -71,6 +73,13 @@ var csv2tabCmd = &cobra.Command{
checkError(record.Err)
}

if handleHeaderRow {
handleHeaderRow = false
if config.NoOutHeader {
continue
}
}

checkError(writer.Write(record.Selected))
}

Expand Down
11 changes: 10 additions & 1 deletion csvtk/cmd/csv2xlsx.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ Attention:
}
}

if !config.NoHeaderRow {
if !config.NoHeaderRow && !config.NoOutHeader {
checkError(xlsx.SetPanes(sheet, &excelize.Panes{
Freeze: true,
Split: false,
Expand All @@ -122,10 +122,19 @@ Attention:
}

line = 1
handleHeaderRow := !config.NoHeaderRow
for record := range csvReader.Ch {
if record.Err != nil {
checkError(record.Err)
}

if handleHeaderRow {
handleHeaderRow = false
if config.NoOutHeader {
continue
}
}

for col, val = range record.Selected {
cell = fmt.Sprintf("%s%d", ExcelColumnIndex(col), line)
if formatNumbers {
Expand Down
8 changes: 8 additions & 0 deletions csvtk/cmd/cut.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,19 @@ Examples:
ShowRowNumber: config.ShowRowNumber,
})

handleHeaderRow := !config.NoHeaderRow
for record := range csvReader.Ch {
if record.Err != nil {
checkError(record.Err)
}

if handleHeaderRow {
handleHeaderRow = false
if config.NoOutHeader {
continue
}
}

writer.Write(record.Selected)
}

Expand Down
19 changes: 15 additions & 4 deletions csvtk/cmd/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ var filterCmd = &cobra.Command{
checkError(writer.Error())
}()

showRowNumber := printLineNumber || config.ShowRowNumber

for _, file := range files {
csvReader, err := newCSVReaderByConfig(config, file)

Expand All @@ -110,7 +112,7 @@ var filterCmd = &cobra.Command{
csvReader.Read(ReadOption{
FieldStr: fieldStr,
FuzzyFields: fuzzyFields,
ShowRowNumber: printLineNumber || config.ShowRowNumber,
ShowRowNumber: showRowNumber,

DoNotAllowDuplicatedColumnName: true,
})
Expand All @@ -120,6 +122,7 @@ var filterCmd = &cobra.Command{
var n int
var v float64
var val string
var i int

checkFirstLine := true
for record := range csvReader.Ch {
Expand All @@ -131,7 +134,10 @@ var filterCmd = &cobra.Command{
checkFirstLine = false

if !config.NoHeaderRow || record.IsHeaderRow { // do not replace head line
if printLineNumber {
if config.NoOutHeader {
continue
}
if showRowNumber {
unshift(&record.All, "row")
}
checkError(writer.Write(record.All))
Expand All @@ -144,7 +150,11 @@ var filterCmd = &cobra.Command{
flag = false
n = 0

for _, val = range record.Selected {
for i, val = range record.Selected {
if showRowNumber && i == 0 { // skip the row number
continue
}

if !reDigitals.MatchString(val) {
flag = false
break
Expand Down Expand Up @@ -187,6 +197,7 @@ var filterCmd = &cobra.Command{
break
}
}

}

if n == len(record.Fields) { // all satisfied
Expand All @@ -196,7 +207,7 @@ var filterCmd = &cobra.Command{
continue
}

if printLineNumber {
if showRowNumber {
unshift(&record.All, strconv.Itoa(record.Row))
}
checkError(writer.Write(record.All))
Expand Down
11 changes: 8 additions & 3 deletions csvtk/cmd/filter2.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@ Custom functions:
checkError(writer.Error())
}()

showRowNumber := printLineNumber || config.ShowRowNumber

for _, file := range files {
csvReader, err := newCSVReaderByConfig(config, file)

Expand All @@ -207,7 +209,7 @@ Custom functions:
FieldStr: fieldStr,
FieldStrSep: varSep,
FuzzyFields: fuzzyFields,
ShowRowNumber: printLineNumber || config.ShowRowNumber,
ShowRowNumber: showRowNumber,

DoNotAllowDuplicatedColumnName: true,
})
Expand Down Expand Up @@ -263,7 +265,10 @@ Custom functions:
colnamesMap[col] = fuzzyField2Regexp(col)
}

if printLineNumber {
if config.NoOutHeader {
continue
}
if showRowNumber {
unshift(&record.All, "row")
}
checkError(writer.Write(record.All))
Expand Down Expand Up @@ -398,7 +403,7 @@ Custom functions:
continue
}

if printLineNumber {
if showRowNumber {
unshift(&record.All, strconv.Itoa(record.Row))
}
checkError(writer.Write(record.All))
Expand Down
3 changes: 3 additions & 0 deletions csvtk/cmd/fmtdate.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ Placeholders:
checkFirstLine = false

if !config.NoHeaderRow || record.IsHeaderRow { // do not replace head line
if config.NoOutHeader {
continue
}
checkError(writer.Write(record.All))
continue
}
Expand Down
3 changes: 3 additions & 0 deletions csvtk/cmd/fold.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ Example:
checkFirstLine = false

if !config.NoHeaderRow || record.IsHeaderRow { // do not replace head line
if config.NoOutHeader {
continue
}
checkError(writer.Write(record.Selected))
continue
}
Expand Down
3 changes: 3 additions & 0 deletions csvtk/cmd/freq.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ var freqCmd = &cobra.Command{
if checkFirstLine {
checkFirstLine = false
if !config.NoHeaderRow || record.IsHeaderRow {
if config.NoOutHeader {
continue
}
checkError(writer.Write(append(record.Selected, "frequency")))
continue
}
Expand Down
5 changes: 3 additions & 2 deletions csvtk/cmd/gather.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,9 @@ var gatherCmd = &cobra.Command{
if handleHeaderRow {
items[nFieldsLeft] = fieldKey
items[nFieldsLeft+1] = fieldValue
checkError(writer.Write(items))

if !config.NoOutHeader {
checkError(writer.Write(items))
}
handleHeaderRow = false
} else {
for _, f = range record.Fields {
Expand Down
3 changes: 3 additions & 0 deletions csvtk/cmd/grep.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,9 @@ Attentions:
checkFirstLine = false

if !config.NoHeaderRow || record.IsHeaderRow {
if config.NoOutHeader {
continue
}
if printLineNumber {
unshift(&record.All, "row")
}
Expand Down
16 changes: 14 additions & 2 deletions csvtk/cmd/head.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ package cmd
import (
"encoding/csv"
"runtime"
"strconv"

"github.com/shenwei356/xopen"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -63,6 +64,8 @@ var headCmd = &cobra.Command{
checkError(writer.Error())
}()

printLineNumber := config.ShowRowNumber

for _, file := range files {
csvReader, err := newCSVReaderByConfig(config, file)

Expand All @@ -87,14 +90,23 @@ var headCmd = &cobra.Command{
checkError(record.Err)
}

checkError(writer.Write(record.All))

if isHeaderLine {
isHeaderLine = false
if config.NoOutHeader {
continue
}
if printLineNumber {
unshift(&record.All, "row")
}
} else {
i++
if printLineNumber {
unshift(&record.All, strconv.Itoa(record.Row))
}
}

checkError(writer.Write(record.All))

if number == i {
break
}
Expand Down
2 changes: 1 addition & 1 deletion csvtk/cmd/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ func getConfigs(cmd *cobra.Command) Config {
Tabs: tabs,
OutTabs: getFlagBool(cmd, "out-tabs"),
NoHeaderRow: noHeaderRow,
NoOutHeader: getFlagBool(cmd, "no-out-header"),
NoOutHeader: getFlagBool(cmd, "delete-header"),

ShowRowNumber: getFlagBool(cmd, "show-row-number"),

Expand Down
2 changes: 1 addition & 1 deletion csvtk/cmd/inter.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ Attention:
return
}

if hasHeaderLine {
if hasHeaderLine && !config.NoOutHeader {
checkError(writer.Write(selectedColnames))
}
for key := range keysMaps {
Expand Down
18 changes: 10 additions & 8 deletions csvtk/cmd/join.go
Original file line number Diff line number Diff line change
Expand Up @@ -530,18 +530,20 @@ Attention:
Data = Data2
}

if withHeaderRow {
if filenameAsPrefix {
if !config.NoOutHeader {
if withHeaderRow {
if filenameAsPrefix {
checkError(writer.Write(prefixedHeaderRow))
} else if addSuffix {
checkError(writer.Write(suffixedHeaderRow))
} else {
checkError(writer.Write(HeaderRow))
}
} else if filenameAsPrefix {
checkError(writer.Write(prefixedHeaderRow))
} else if addSuffix {
checkError(writer.Write(suffixedHeaderRow))
} else {
checkError(writer.Write(HeaderRow))
}
} else if filenameAsPrefix {
checkError(writer.Write(prefixedHeaderRow))
} else if addSuffix {
checkError(writer.Write(suffixedHeaderRow))
}
for _, record := range Data {
checkError(writer.Write(record))
Expand Down
6 changes: 4 additions & 2 deletions csvtk/cmd/mutate.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ var mutateCmd = &cobra.Command{
}

name := getFlagString(cmd, "name")
if !config.NoHeaderRow && name == "" {
if !config.NoHeaderRow && name == "" && !config.NoOutHeader {
checkError(fmt.Errorf("flag -n (--name) needed"))
}

Expand Down Expand Up @@ -204,7 +204,9 @@ var mutateCmd = &cobra.Command{
}

handleHeaderRow = false
checkError(writer.Write(record2))
if !config.NoOutHeader {
checkError(writer.Write(record2))
}
continue
}

Expand Down
6 changes: 4 additions & 2 deletions csvtk/cmd/mutate2.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ Custom functions:
runtime.GOMAXPROCS(config.NumCPUs)

name := getFlagString(cmd, "name")
if !config.NoHeaderRow && name == "" {
if !config.NoHeaderRow && name == "" && !config.NoOutHeader {
checkError(fmt.Errorf("falg -n (--name) needed"))
}

Expand Down Expand Up @@ -300,7 +300,9 @@ Custom functions:
record2[at-1] = value
}

checkError(writer.Write(record2))
if !config.NoOutHeader {
checkError(writer.Write(record2))
}

continue
}
Expand Down
Loading

0 comments on commit 0eb045e

Please sign in to comment.