Skip to content

Instantly share code, notes, and snippets.

@maning

maning/split.sh Secret

Created July 16, 2019 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maning/8a381a66e4f245429d67a6f39e205e24 to your computer and use it in GitHub Desktop.
Save maning/8a381a66e4f245429d67a6f39e205e24 to your computer and use it in GitHub Desktop.
#! /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