Skip to content

Instantly share code, notes, and snippets.

@icio
Created September 24, 2011 17:56
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 icio/1239633 to your computer and use it in GitHub Desktop.
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
#!/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