Skip to content

Instantly share code, notes, and snippets.

@SeungheonOh
Last active February 19, 2020 03:02
Show Gist options
  • Save SeungheonOh/7fa06ea926f42c577278434371ae8b1e to your computer and use it in GitHub Desktop.
Save SeungheonOh/7fa06ea926f42c577278434371ae8b1e to your computer and use it in GitHub Desktop.
CSV spliter
package main
import (
"bufio"
"errors"
"fmt"
"os"
"strconv"
"sync"
)
func NewFileFromLine(input_file string, output_file string, start int, length int, wg *sync.WaitGroup) error {
defer wg.Done()
file_out, err := os.Create(output_file)
defer file_out.Close()
if err != nil {
return err
}
fmt.Println(file_out.Name())
file_in, err := os.Open(input_file)
defer file_in.Close()
if err != nil {
return err
}
scanner := bufio.NewScanner(file_in)
scanner.Split(bufio.ScanLines)
output := bufio.NewWriter(file_out)
scanner.Scan()
output.WriteString(scanner.Text() + "\n")
// Skip line until the desired line count
for i := 1; i < start; i++ {
if !scanner.Scan() {
return errors.New("Invalid length")
}
}
for i := 0; i <= length && scanner.Scan(); i++ {
output.WriteString(scanner.Text() + "\n")
}
output.Flush()
return nil
}
func main() {
file_target := os.Args[1]
_ = file_target
incre, err := strconv.Atoi(os.Args[2])
if err != nil {
panic(err)
}
s, err := strconv.Atoi(os.Args[3])
if err != nil {
panic(err)
}
var wg sync.WaitGroup
for i := 0; i <= s/incre; i++ {
wg.Add(1)
fmt.Println("Worker spawned", i)
go NewFileFromLine(file_target, file_target+strconv.Itoa(i), i*incre+1, incre-1, &wg)
}
wg.Wait()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment