shenwei356 / csvtk

A cross-platform, efficient and practical CSV/TSV toolkit in Golang
http://bioinf.shenwei.me/csvtk
MIT License
992 stars 84 forks source link

concat throws an error if the first supplied file is empty #259

Closed MostafaYA closed 9 months ago

MostafaYA commented 9 months ago

Prerequisites

Describe your issue

#generate files
echo -e "1,2,3\n1,2,3" > data1.csv 
echo -e "4,5,6\n4,5,6" > data2.csv 
touch empty.csv

#concat files  (first file is not empty)
csvtk concat -H data1.csv data2.csv empty.csv 
[WARN] csvtk concat: skipping empty input file: empty.csv
1,2,3
1,2,3
4,5,6
4,5,6

#concat files  (first file is empty)
csvtk concat -H empty.csv data1.csv data2.csv
[WARN] csvtk concat: skipping empty input file: empty.csv
panic: runtime error: index out of range [0] with length 0

goroutine 1 [running]:
github.com/shenwei356/csvtk/csvtk/cmd.glob..func5(0x180a3a0?, {0xc000539180, 0x3, 0x4})
        /home/shenwei/go/src/github.com/shenwei356/csvtk/csvtk/cmd/concat.go:129 +0x929
github.com/spf13/cobra.(*Command).execute(0x180a3a0, {0xc0005390c0, 0x4, 0x4})
        /home/shenwei/go/pkg/mod/github.com/spf13/cobra@v1.7.0/command.go:944 +0x847
github.com/spf13/cobra.(*Command).ExecuteC(0x1809820)
        /home/shenwei/go/pkg/mod/github.com/spf13/cobra@v1.7.0/command.go:1068 +0x3bd
github.com/spf13/cobra.(*Command).Execute(...)
        /home/shenwei/go/pkg/mod/github.com/spf13/cobra@v1.7.0/command.go:992
github.com/shenwei356/csvtk/csvtk/cmd.Execute()
        /home/shenwei/go/src/github.com/shenwei356/csvtk/csvtk/cmd/root.go:72 +0x25
main.main()
        /home/shenwei/go/src/github.com/shenwei356/csvtk/csvtk/main.go:59 +0x17

Thank you

I'm grateful to users who have greatly helped to report bugs and suggested new features.

I may respond to issues or fix bugs quickly, but I usually implement new features periodically (two or more weeks).

shenwei356 commented 9 months ago

Thanks for reporting this. I've fixed it last week.

csvtk_darwin_amd64.tar.gz csvtk_darwin_arm64.tar.gz csvtk_linux_amd64.tar.gz

MostafaYA commented 9 months ago

This seems not resolved and depends of the order of input files. If first file is empty, it says 'no input data'

#data
echo -e "1,2,3\n1,2,3" > data1.csv 
echo -e "4,5,6\n4,5,6" > data2.csv 
touch empty.csv

$ csvtk concat -H data1.csv data2.csv empty.csv 
[WARN] csvtk concat: skipping empty input file: empty.csv
1,2,3
1,2,3
4,5,6
4,5,6

$ csvtk concat -H empty.csv data1.csv data2.csv
[WARN] csvtk concat: skipping empty input file: empty.csv
[WARN] csvtk concat: no input data
shenwei356 commented 9 months ago

You're right.

csvtk_darwin_amd64.tar.gz csvtk_darwin_arm64.tar.gz csvtk_linux_amd64.tar.gz