|
1 | | -# splitcsv |
2 | | -A lightweight Go utility to split large CSV files into two parts. Supports custom delimiters, optional headers in each output, and manual split size. Useful for handling big datasets without loading the entire file into memory. |
| 1 | +# CSV Splitter |
| 2 | + |
| 3 | +A simple command-line tool to split large CSV files into multiple smaller parts with customizable options. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- Split CSV files into any number of parts |
| 8 | +- Automatically generates output filenames with `_part1`, `_part2`, etc. suffixes |
| 9 | +- Preserves original file extension and directory |
| 10 | +- Optional header inclusion in all output files |
| 11 | +- Support for custom column separators |
| 12 | +- Even distribution of rows across parts |
| 13 | +- Handles files with any number of rows efficiently |
| 14 | + |
| 15 | +## Installation |
| 16 | + |
| 17 | +```bash |
| 18 | +go build -o splitcsv main.go |
| 19 | +``` |
| 20 | + |
| 21 | +## Usage |
| 22 | + |
| 23 | +```bash |
| 24 | +./splitcsv -in <input-file> [options] |
| 25 | +``` |
| 26 | + |
| 27 | +### Required Flags |
| 28 | + |
| 29 | +- `-in` - Input CSV file path (required) |
| 30 | + |
| 31 | +### Optional Flags |
| 32 | + |
| 33 | +- `-parts` - Number of parts to split into (default: 2) |
| 34 | +- `-header` - Include header row in all output files (default: true) |
| 35 | +- `-comma` - Column separator character (default: ",") |
| 36 | + |
| 37 | +## Examples |
| 38 | + |
| 39 | +### Basic Usage |
| 40 | + |
| 41 | +Split a CSV file into 2 parts (default): |
| 42 | +```bash |
| 43 | +./splitcsv -in data.csv |
| 44 | +``` |
| 45 | +Output: `data_part1.csv`, `data_part2.csv` |
| 46 | + |
| 47 | +### Split into Multiple Parts |
| 48 | + |
| 49 | +Split into 5 parts: |
| 50 | +```bash |
| 51 | +./splitcsv -in sales_data.csv -parts 5 |
| 52 | +``` |
| 53 | +Output: `sales_data_part1.csv`, `sales_data_part2.csv`, ..., `sales_data_part5.csv` |
| 54 | + |
| 55 | +### Without Headers |
| 56 | + |
| 57 | +Split without including headers in output files: |
| 58 | +```bash |
| 59 | +./splitcsv -in data.csv -parts 3 -header=false |
| 60 | +``` |
| 61 | + |
| 62 | +### Custom Separator |
| 63 | + |
| 64 | +Split a semicolon-separated file: |
| 65 | +```bash |
| 66 | +./splitcsv -in european_data.csv -parts 4 -comma ";" |
| 67 | +``` |
| 68 | + |
| 69 | +### Complex Example |
| 70 | + |
| 71 | +Split a large file with tab separator into 10 parts without headers: |
| 72 | +```bash |
| 73 | +./splitcsv -in huge_dataset.tsv -parts 10 -comma "\t" -header=false |
| 74 | +``` |
| 75 | + |
| 76 | +## How It Works |
| 77 | + |
| 78 | +1. **Row Counting**: First pass counts total data rows (excluding header) |
| 79 | +2. **Distribution**: Calculates optimal row distribution across parts |
| 80 | +3. **File Generation**: Creates output files with `_partN` suffix |
| 81 | +4. **Data Writing**: Distributes rows evenly, with extra rows going to first parts |
| 82 | + |
| 83 | +### Row Distribution Logic |
| 84 | + |
| 85 | +For a file with 100 data rows split into 3 parts: |
| 86 | +- Part 1: 34 rows |
| 87 | +- Part 2: 33 rows |
| 88 | +- Part 3: 33 rows |
| 89 | + |
| 90 | +Extra rows are distributed to the first parts to ensure even splitting. |
| 91 | + |
| 92 | +## File Naming Convention |
| 93 | + |
| 94 | +Output files follow this pattern: |
| 95 | +``` |
| 96 | +{original_name}_part{N}{original_extension} |
| 97 | +``` |
| 98 | + |
| 99 | +Examples: |
| 100 | +- `data.csv` → `data_part1.csv`, `data_part2.csv` |
| 101 | +- `sales_2024.csv` → `sales_2024_part1.csv`, `sales_2024_part2.csv` |
| 102 | +- `export.tsv` → `export_part1.tsv`, `export_part2.tsv` |
| 103 | + |
| 104 | +## Error Handling |
| 105 | + |
| 106 | +The tool will exit with an error message if: |
| 107 | +- Input file doesn't exist or can't be read |
| 108 | +- Input file has no data rows |
| 109 | +- Number of parts is less than 1 |
| 110 | +- Separator is not a single character |
| 111 | +- Output files can't be created |
| 112 | + |
| 113 | +## Performance |
| 114 | + |
| 115 | +- Memory efficient: processes files row by row |
| 116 | +- Two-pass reading: first for counting, second for splitting |
| 117 | +- Supports files of any size (limited only by available disk space) |
| 118 | + |
| 119 | +## Requirements |
| 120 | + |
| 121 | +- Go 1.16 or later |
| 122 | +- Read permission for input file |
| 123 | +- Write permission for output directory |
| 124 | + |
| 125 | +## License |
| 126 | + |
| 127 | +This project is open source and available under the MIT License. |
0 commit comments