Skip to content

Instantly share code, notes, and snippets.

@maning

maning/split.sh Secret

Created Jul 16, 2019
Embed
What would you like to do?
#! /bin/bash
# Split files according to specified percentage.
# ./split.sh brown.txt 60 20 20
# you also can use the placeholder . which fills the percentage up to 100%.
#./split.sh brown.txt 60 20 .
# https://stackoverflow.com/questions/40415960/how-to-split-file-by-percentage-of-no-of-lines
file="$1"
fileLength=$(wc -l < "$file")
shift
part=1
percentSum=0
currentLine=1
for percent in "$@"; do
[ "$percent" == "." ] && ((percent = 100 - percentSum))
((percentSum += percent))
if ((percent < 0 || percentSum > 100)); then
echo "invalid percentage" 1>&2
exit 1
fi
((nextLine = fileLength * percentSum / 100))
if ((nextLine < currentLine)); then
printf "" # create empty file
else
sed -n "$currentLine,$nextLine"p "$file"
fi > "part$part-$file"
((currentLine = nextLine + 1))
((part++))
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment