Created
September 24, 2011 17:56
-
-
Save icio/1239633 to your computer and use it in GitHub Desktop.
Split (by line) a file into similarly-sized parts each less than a given size
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
#!/usr/bin/env bash | |
# Filesize limit (MiB) | |
limit=$(($1 * 1000**2)) | |
shift | |
# Accept multiple files in the arguments | |
for file in $@ | |
do | |
# Skip arguments that aren't regular files | |
if [ ! -f $file ] | |
then | |
echo Skipping $file | |
continue | |
fi | |
# File counts | |
size=$(ls -l $file | awk '{ print $5 }') | |
lines=$(wc -l $file | awk '{ print $1 }') | |
parts=$((1 + $size/$limit)) | |
linelimit=$((1 + $lines/$parts)) | |
echo $file $parts parts $linelimit lines per part | |
split -l $linelimit $file "$file." | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment