Created
June 14, 2017 14:41
-
-
Save stevenwilkin/bda28e9e73816fafaaf9ee03e0451ecd to your computer and use it in GitHub Desktop.
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
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment