Skip to content

Commit

Permalink
Merge pull request #17 from onozaty/develop/v1.11.0
Browse files Browse the repository at this point in the history
Develop v1.11.0
  • Loading branch information
onozaty committed Sep 12, 2021
2 parents a77da5b + d25a97c commit 31a75e3
Show file tree
Hide file tree
Showing 6 changed files with 898 additions and 20 deletions.
102 changes: 83 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@
* [remove](#remove) Remove columns.
* [rename](#rename) Rename columns.
* [replace](#replace) Replace values.
* [sort](#sort) Sort rows.
* [slice](#slice) Slice specified range of rows.
* [sort](#sort) Sort rows.
* [split](#split) Split into multiple CSV files.
* [transform](#transform) Transform format.
* [unique](#unique) Extract unique rows.

Expand Down Expand Up @@ -814,6 +815,68 @@ Please refer to the following for the syntax of regular expressions.

* https://golang.org/pkg/regexp/syntax/

## slice

Create a new CSV file by slicing the specified range of rows from the input CSV file.

### Usage

```
csvt slice -i INPUT [-s START] [-e END] -o OUTPUT
```

```
Usage:
csvt slice [flags]
Flags:
-i, --input string Input CSV file path.
-s, --start int The number of the starting row. If not specified, it will be the first row. (default 1)
-e, --end int The number of the end row. If not specified, it will be the last row. (default 2147483647)
-o, --output string Output CSV file path.
-h, --help help for slice
```

### Example

The contents of `input.csv`.

```
ID,Name
1,name1
2,name2
3,name3
4,name4
5,name5
```

Slice the second through fourth records.

```
$ csvt slice -i input.csv -s 2 -e 4 -o output.csv
```

The contents of the created `output.tsv`.

```
ID,Name
2,name2
3,name3
4,name4
```

The `-s` and `-e` can be omitted.
If you want to extract the first row, it is sufficient to specify only `-e`, as shown below.

```
$ csvt slice -i input.csv -e 1 -o output.csv
```

```
ID,Name
1,name1
```

## sort

Creates a new CSV file from the input CSV file by sorting by the values in the specified columns.
Expand Down Expand Up @@ -897,26 +960,26 @@ col1
123
```

## slice
## split

Create a new CSV file by slicing the specified range of rows from the input CSV file.
Split the CSV file by the specified number of rows.

### Usage

```
csvt slice -i INPUT [-s START] [-e END] -o OUTPUT
csvt split -i INPUT -r ROWS -o OUTPUT
```

```
Usage:
csvt slice [flags]
csvt split [flags]
Flags:
-i, --input string Input CSV file path.
-s, --start int The number of the starting row. If not specified, it will be the first row. (default 1)
-e, --end int The number of the end row. If not specified, it will be the last row. (default 2147483647)
-o, --output string Output CSV file path.
-h, --help help for slice
-r, --rows int Maximum number of rows.
-o, --output string Output CSV file base path. If you specify "output.csv", the file will be output as "output-1.csv" "output-2.csv" ...
It is also possible to specify the position of the embedded serial number in "%d".
-h, --help help for split
```

### Example
Expand All @@ -932,31 +995,32 @@ ID,Name
5,name5
```

Slice the second through fourth records.
Split into multiple CSV files every two rows.

```
$ csvt slice -i input.csv -s 2 -e 4 -o output.csv
$ csvt split -i input.csv -r 2 -o output.csv
```

The contents of the created `output.tsv`.
It will be split into the following three files.

`output-1.csv`
```
ID,Name
1,name1
2,name2
3,name3
4,name4
```

The `-s` and `-e` can be omitted.
If you want to extract the first row, it is sufficient to specify only `-e`, as shown below.

`output-2.csv`
```
$ csvt slice -i input.csv -e 1 -o output.csv
ID,Name
3,name3
4,name4
```

`output-3.csv`
```
ID,Name
1,name1
5,name5
```

## transform
Expand Down
36 changes: 35 additions & 1 deletion cmd/common_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package cmd

import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
)
Expand All @@ -26,16 +28,48 @@ func createTempFile(t *testing.T, content string) string {
return tempFile.Name()
}

func readString(t *testing.T, name string) string {
func createTempDir(t *testing.T) string {

tempDir, err := os.MkdirTemp("", "csvt")
if err != nil {
t.Fatal("craete dir failed\n", err)
}

return tempDir
}

func readBytes(t *testing.T, name string) []byte {

bo, err := os.ReadFile(name)
if err != nil {
t.Fatal("read failed\n", err)
}

return bo
}

func readString(t *testing.T, name string) string {

bo := readBytes(t, name)
return string(bo)
}

func joinRows(rows ...string) string {
return strings.Join(rows, "\r\n") + "\r\n"
}

func readDir(t *testing.T, dir string) map[string][]byte {

files, err := ioutil.ReadDir(dir)
if err != nil {
t.Fatal("read dir failed\n", err)
}

contentsMap := map[string][]byte{}
for _, f := range files {
b := readBytes(t, filepath.Join(dir, f.Name()))
contentsMap[f.Name()] = b
}

return contentsMap
}
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func newRootCmd() *cobra.Command {
rootCmd.AddCommand(newSliceCmd())
rootCmd.AddCommand(newAddCmd())
rootCmd.AddCommand(newSortCmd())
rootCmd.AddCommand(newSplitCmd())

for _, c := range rootCmd.Commands() {
// フラグ以外は受け付けないように
Expand Down
1 change: 1 addition & 0 deletions cmd/sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ func sort(reader csv.CsvReader, targetColumnNames []string, writer csv.CsvWriter
if err != nil {
return err
}
defer sortedRows.Close()

err = writer.Write(sortedRows.ColumnNames())
if err != nil {
Expand Down
Loading

0 comments on commit 31a75e3

Please sign in to comment.