Skip to content

Instantly share code, notes, and snippets.

@stevenwilkin
Created June 14, 2017 14:41
Show Gist options
  • Save stevenwilkin/bda28e9e73816fafaaf9ee03e0451ecd to your computer and use it in GitHub Desktop.
Save stevenwilkin/bda28e9e73816fafaaf9ee03e0451ecd to your computer and use it in GitHub Desktop.

Revisions

  1. stevenwilkin created this gist Jun 14, 2017.
    26 changes: 26 additions & 0 deletions split.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    #!/bin/bash

    # create sample file
    original=original.txt
    rm -f $original
    touch $original

    for x in $(seq 100); do
    echo $x >> $original
    done

    # split file
    total_lines=$(wc -l $original | awk '{ print $1 }')
    lines_per_file=10

    number_files=$((total_lines / lines_per_file))
    if [ $((total_lines % lines_per_file)) -ne 0 ]; then
    number_files=$(($number_files + 1))
    fi

    for x in $(seq $number_files); do
    start=$(((($x - 1) * $lines_per_file) + 1))
    end=$(($start + $lines_per_file - 1 ))
    output=$x.txt
    sed -n "${start},${end}p" $original > $output
    done