Split Excel CSV into Multiple files using Bash script

Split Excel CSV into Multiple files using Bash script. For example if you have a csv with more than 15000 products, and you have to split it into small file with 100 or 200 products in each file. It can be very difficult.In this article, i wrote an Bash Script, to Split Excel CSV into Multiple files using Bash script.

Split Excel CSV into Multiple files using Bash script

For this, we have to create a hash script file, named as split.sh
And write the following lines to it.

#!/bin/bash
tail -n +2 filename.csv | split -l 200 - splited_files
COUNTER=36
START=36
for file in splited_files*
do
    head -n 1 filename.csv > tmp_file
    cat $file >> tmp_file
    # mv -f tmp_file $file_$COUNTER."csv" //use this for full file name
    mv -f tmp_file $file_$COUNTER."csv"
    COUNTER=$((COUNTER+1))
    rm $file
done
echo "Total Files Created:" $((COUNTER-START))

Ok.Lets go little bit deep into the code.

tail -n +2 filename.csv | split -l 200 - splited_files

Give the filename of the CSV/TXT. For example file.csv or file.txt
You can give values of counter or just avoid that part.
If you number your splitted files with name you can give the total number of files that you are expecting to in the COUNTER variable.
And the starting number of file in the the start variable.

To Run:
Place the split.sh in the exact same folder of the csv/txt file.
Open a terminal in the same folder and type :

 sh split.sh

You can see the splitted files in the same folder. If you can’t see just refresh the folder using the function key F5.

Download the Split.sh file from here : Download here

[wpdm_package id=’77’]

I hope you got enough knowledge from this article to Split Excel CSV into Multiple files using Bash script.If you have any doubt, drop below. Thanks for reading.