Skip to content

Instantly share code, notes, and snippets.

@jfrobbins
Last active August 29, 2015 13:57
Show Gist options
  • Select an option

  • Save jfrobbins/9755028 to your computer and use it in GitHub Desktop.

Select an option

Save jfrobbins/9755028 to your computer and use it in GitHub Desktop.
//syntax:
// ./filesplitter -file filename.csv -lines=150
//
// source for readlines() and writelines() from here:
// http://stackoverflow.com/questions/8757389/reading-file-line-by-line-in-go
package main
import (
"os"
"bufio"
"bytes"
"io"
"fmt"
"strings"
"strconv"
"flag"
)
// Read a whole file into the memory and store it as array of lines
func readLines(path string) (lines []string, err error) {
var (
file *os.File
part []byte
prefix bool
)
if file, err = os.Open(path); err != nil {
return
}
defer file.Close()
reader := bufio.NewReader(file)
buffer := bytes.NewBuffer(make([]byte, 0))
for {
if part, prefix, err = reader.ReadLine(); err != nil {
break
}
buffer.Write(part)
if !prefix {
lines = append(lines, buffer.String())
buffer.Reset()
}
}
if err == io.EOF {
err = nil
}
return
}
func writeLines(lines []string, path string) (err error) {
var (
file *os.File
)
if file, err = os.Create(path); err != nil {
return
}
defer file.Close()
//writer := bufio.NewWriter(file)
for _,item := range lines {
//fmt.Println(item)
_, err := file.WriteString(strings.TrimSpace(item) + "\n");
//file.Write([]byte(item));
if err != nil {
//fmt.Println("debug")
fmt.Println(err)
break
}
}
/*content := strings.Join(lines, "\n")
_, err = writer.WriteString(content)*/
return
}
func main() {
var inFile string
flag.StringVar(&inFile, "file", "fakedata.csv", "the file to split")
var linesPerFile = flag.Int("lines", 10, "the number of lines per out file")
flag.Parse()
fmt.Println("lines per File: " + strconv.Itoa(*linesPerFile))
lines, err := readLines(inFile)
if err != nil {
fmt.Println("Error: %s\n", err)
return
}
header := lines[0]
//fmt.Println(header)
for nFile:=0; nFile < (len(lines) / *linesPerFile); nFile++ {
startLn := nFile * *linesPerFile + 1
stopLn := startLn + *linesPerFile - 1
//copy data to an out chunk
// (gotta be a better way to do this?
ochunk := make([]string, *linesPerFile + 1)
ochunk[0] = header
fmt.Println(ochunk[0])
chunkCount := 1
for i := startLn; i <= stopLn; i++ {
ochunk[chunkCount] = lines[i]
fmt.Println(ochunk[chunkCount])
chunkCount++
}
//write the chunk to file:
err = writeLines(ochunk, inFile + "_" + strconv.Itoa(nFile) + ".csv")
fmt.Println(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment