Skip to content

Instantly share code, notes, and snippets.

@ubermuda
Created November 30, 2011 10:46
Show Gist options
  • Save ubermuda/1408635 to your computer and use it in GitHub Desktop.
Save ubermuda/1408635 to your computer and use it in GitHub Desktop.
A small shellscript to slice a file into several smaller files
#!/bin/bash
INPUT_FILE=$1
LINES_PER_FILE=$2
if [ -z $LINES_PER_FILE ]; then
LINES_PER_FILE=1000
fi
if [ -z $INPUT_FILE ]; then
echo "usage: $0 input_file [lines_per_file=1000]"
exit 1
fi
if [ ! -f $INPUT_FILE ]; then
echo "$INPUT_FILE could not be read"
exit 1
fi
INPUT_LINES=`wc -l $INPUT_FILE | awk '{print $1}'`
if [ $INPUT_LINES -lt $LINES_PER_FILE ]; then
echo "input files has less than $LINES_PER_FILE lines"
exit 0
fi
RESULT_FILES=$((($INPUT_LINES / $LINES_PER_FILE) + 1))
echo "slicing $INPUT_FILE into $RESULT_FILES $LINES_PER_FILE lines files "
for i in `seq $RESULT_FILES`; do
echo "slicing file #$i"
tail -n +$((($i - 1) * $LINES_PER_FILE)) $INPUT_FILE | head -n $LINES_PER_FILE > $INPUT_FILE.$i
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment