Skip to content

Instantly share code, notes, and snippets.

@gordonmurray
Created April 16, 2017 18:29
Show Gist options
  • Save gordonmurray/58cb12f2f58fa59259bc73942175e2eb to your computer and use it in GitHub Desktop.
Save gordonmurray/58cb12f2f58fa59259bc73942175e2eb to your computer and use it in GitHub Desktop.
A small script to take a larger text based file and break it down to several smaller files
#!/usr/bin/env bash
# A small script to take a larger text based file and break it down to several smaller files
# of 500 lines each for easier processing, keeping the headings at the top of each file
FILENAME=${1}
if [ -z ${FILENAME} ]; then
echo "Please provide a filename, for example: bash split.sh /path/to/data.csv";
else
CLEAN_FILENAME="${FILENAME/-*\./.}"
CLEAN_FILENAME=${CLEAN_FILENAME%.*}
tail -n +2 ${FILENAME} | split -a 5 -d -l 500 - ${CLEAN_FILENAME}_
for file in ${CLEAN_FILENAME}_*
do
head -n 1 ${FILENAME} > tmp_file
cat ${file} >> tmp_file
mv -f tmp_file ${file}.csv
rm ${file}
done
echo ${CLEAN_FILENAME}
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment