-
-
Save maning/8a381a66e4f245429d67a6f39e205e24 to your computer and use it in GitHub Desktop.
This file contains 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 | |
# 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