Created
April 22, 2014 05:14
-
-
Save horsley/11166010 to your computer and use it in GitHub Desktop.
文件分割器 for jingboli
This file contains hidden or 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
// ljbSpilter project main.go | |
package main | |
import ( | |
"bufio" | |
"fmt" | |
"os" | |
"path/filepath" | |
"strconv" | |
"strings" | |
) | |
func main() { | |
fmt.Println("*******************************************") | |
fmt.Println("* 文件分割器 for jingboli *") | |
fmt.Println("* by horsleyli *") | |
fmt.Println("*******************************************\n") | |
if len(os.Args) == 1 { | |
fmt.Println("arg 1: file \n[arg 2: delimter default=#] \n[arg 3: field index default=0]") | |
os.Exit(1) | |
} | |
filename := os.Args[1] | |
delimter := "#" | |
field := 0 | |
if len(os.Args) == 3 { | |
delimter = os.Args[2] | |
} | |
if len(os.Args) == 4 { | |
field, _ = strconv.Atoi(os.Args[3]) | |
} | |
var inputFile, outputFile *os.File | |
var newFile string | |
var err error | |
if inputFile, err = os.Open(filename); err != nil { | |
fmt.Println("输入文件打开失败!" + err.Error()) | |
os.Exit(1) | |
} | |
defer inputFile.Close() | |
scanner := bufio.NewScanner(inputFile) | |
ext := filepath.Ext(filename) | |
rs := []rune(filename) | |
for scanner.Scan() { | |
if newFile == "" { | |
newFile = string(rs[0:len(rs)-len(ext)]) + "_field_" + strconv.Itoa(field) + ext | |
//fmt.Println(newFile) | |
if outputFile, err = os.Create(newFile); err != nil { | |
fmt.Println("输出文件打开失败!" + err.Error()) | |
os.Exit(1) | |
} | |
} | |
all_fields := strings.Split(scanner.Text(), delimter) | |
outputFile.WriteString(all_fields[field] + "\r\n") | |
} | |
outputFile.Close() | |
fmt.Println("写出文件:" + newFile) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment