-
-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 66989e2
Showing
14 changed files
with
858 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Compiled Object files, Static and Dynamic libs (Shared Objects) | ||
*.o | ||
*.a | ||
*.so | ||
|
||
# Folders | ||
_obj | ||
_test | ||
|
||
# Architecture specific extensions/prefixes | ||
*.[568vq] | ||
[568vq].out | ||
|
||
*.cgo1.go | ||
*.cgo2.c | ||
_cgo_defun.c | ||
_cgo_gotypes.go | ||
_cgo_export.* | ||
|
||
_testmain.go | ||
|
||
*.exe | ||
*.test | ||
*.prof | ||
*.directory | ||
doc/site/* | ||
*.Rhistory |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright © 2016 Wei Shen | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
csvtk | ||
|
||
Another cross-platform, efficient and practical CSV/TSV tool kit. | ||
|
||
## commands | ||
|
||
**Information** | ||
|
||
- [x] stat | ||
|
||
**Format convertion** | ||
|
||
- [x] csv2tab | ||
- [x] tab2csv | ||
- space2tab | ||
- [x] transpose | ||
|
||
**Set operations** | ||
|
||
- cut | ||
- split | ||
- grep | ||
|
||
|
||
- join | ||
- uniq | ||
- inter | ||
|
||
**Ordering** | ||
|
||
- sort | ||
|
||
## Contact | ||
|
||
Email me for any problem when using `csvtk`. shenwei356(at)gmail.com | ||
|
||
[Create an issue](https://github.com/shenwei356/csvtk/issues) to report bugs, | ||
propose new functions or ask for help. | ||
|
||
## License | ||
|
||
[MIT License](https://github.com/shenwei356/csvtk/blob/master/LICENSE) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
// Copyright © 2016 Wei Shen <[email protected]> | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
|
||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
|
||
"github.com/brentp/xopen" | ||
"github.com/mzimmerman/multicorecsv" | ||
) | ||
|
||
// CSVRecordsChunk is chunk of CSV records | ||
type CSVRecordsChunk struct { | ||
ID uint64 | ||
Data [][]string | ||
Err error | ||
} | ||
|
||
// CSVReader is | ||
type CSVReader struct { | ||
Reader *multicorecsv.Reader | ||
|
||
bufferSize int | ||
chunkSize int | ||
Ch chan CSVRecordsChunk | ||
|
||
fh *xopen.Reader | ||
} | ||
|
||
// NewCSVReader is | ||
func NewCSVReader(file string, bufferSize int, chunkSize int) (*CSVReader, error) { | ||
if bufferSize < 1 { | ||
return nil, fmt.Errorf("value of bufferSize should be greater than 0") | ||
} | ||
if chunkSize < 1 { | ||
return nil, fmt.Errorf("value of chunkSize should be greater than 0") | ||
} | ||
|
||
fh, err := xopen.Ropen(file) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
reader := multicorecsv.NewReader(fh) | ||
|
||
ch := make(chan CSVRecordsChunk, bufferSize) | ||
|
||
csvReader := &CSVReader{ | ||
Reader: reader, | ||
bufferSize: bufferSize, | ||
chunkSize: chunkSize, | ||
Ch: ch, | ||
fh: fh, | ||
} | ||
return csvReader, nil | ||
} | ||
|
||
// Run begins to read | ||
func (csvReader *CSVReader) Run() { | ||
go func() { | ||
defer func() { | ||
csvReader.fh.Close() | ||
csvReader.Reader.Close() | ||
}() | ||
|
||
chunkData := make([][]string, csvReader.chunkSize) | ||
var id uint64 | ||
var i int | ||
for { | ||
record, err := csvReader.Reader.Read() | ||
if err == io.EOF { | ||
id++ | ||
csvReader.Ch <- CSVRecordsChunk{id, chunkData[0:i], nil} | ||
break | ||
} | ||
if err != nil { | ||
csvReader.Ch <- CSVRecordsChunk{id, chunkData[0:i], err} | ||
break | ||
} | ||
|
||
chunkData[i] = record | ||
i++ | ||
if i == csvReader.chunkSize { | ||
id++ | ||
csvReader.Ch <- CSVRecordsChunk{id, chunkData, nil} | ||
|
||
chunkData = make([][]string, csvReader.chunkSize) | ||
i = 0 | ||
} | ||
} | ||
close(csvReader.Ch) | ||
}() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Copyright © 2016 Wei Shen <[email protected]> | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
|
||
package cmd | ||
|
||
import ( | ||
"encoding/csv" | ||
|
||
"github.com/brentp/xopen" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// csv2tabCmd represents the seq command | ||
var csv2tabCmd = &cobra.Command{ | ||
Use: "csv2tab", | ||
Short: "convert CSV to tabular format", | ||
Long: `convert CSV to tabular format | ||
`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
config := getConfigs(cmd) | ||
files := getFileList(args) | ||
|
||
outfh, err := xopen.Wopen(config.OutFile) | ||
checkError(err) | ||
defer outfh.Close() | ||
|
||
writer := csv.NewWriter(outfh) | ||
writer.Comma = '\t' | ||
|
||
for _, file := range files { | ||
csvReader, err := newCSVReaderByConfig(config, file) | ||
checkError(err) | ||
csvReader.Run() | ||
|
||
for chunk := range csvReader.Ch { | ||
checkError(chunk.Err) | ||
|
||
for _, record := range chunk.Data { | ||
checkError(writer.Write(record)) | ||
} | ||
} | ||
} | ||
writer.Flush() | ||
checkError(writer.Error()) | ||
}, | ||
} | ||
|
||
func init() { | ||
RootCmd.AddCommand(csv2tabCmd) | ||
} |
Oops, something went wrong.