Splitting a Text File or a CSV by Half with bash on Linux or OS X

Here's a quick rundown of the script we'll be using under OS X (Mac OS) or Linux with bash:

total_lines=$(wc -l < unsplitted.csv) 
echo $total_lines 
half_lines=$((total_lines / 2)) 
echo $half_lines 
tail -n +$((half_lines + 1)) unsplitted.csv. > splitted.csv

But what does it mean? Let's break it down:

  • total_lines=$(wc -l < unsplitted.csv): This counts the total number of lines in the file unsplitted.csv.
  • echo $total_lines: This prints the total number of lines on the screen.
  • half_lines=$((total_lines / 2)): This calculates half of the total lines.
  • echo $half_lines: This prints the number of lines in the first half.
  • tail -n +$((half_lines + 1)) unsplitted.csv > splitted.csv: This creates a new file, splitted.csv, with the second half of the lines from unsplitted.csv.

Running the Script

To run the script, simply paste it in your text editor and save. In terminal go to the folder with your script and add execution rights to your newly created file with chmod +x filename command. Then run it with command  ./filename and watch the magic happen. You should now have a new file, splitted.csv, containing the second half of the lines from your original file. Voila!

Analyzing the Script Output
Check the new file to verify the script worked correctly. The number of lines in splitted.csv should be approximately half of those in the original file.

Use Cases of the Script

Data Analysis
One of the most common use-cases for this script is in data analysis. Often, analysts have to deal with enormous datasets. This script can help them split these into manageable parts, making their jobs easier.

File Management
For those dealing with document management, this script can prove invaluable. Splitting large text files into manageable halves can greatly ease the process of file handling.

Troubleshooting Common Issues

Encountering Errors
Now, we've all been there. Running a script and something doesn't work. Here are a couple of common issues you might face and how to troubleshoot them:
  1. If you get a 'no such file or directory' error, double-check the file name and path. Ensure the file exists in the directory you're running the script from.
  2. If the output file has an incorrect number of lines, re-check your script for any typos or syntax errors.
Back to articles


Explore Aber Vulgue's blog for expert articles on programming, software development, and DevOps. Dive into practical guides on JavaScript, C++, mobile app development, neural networks, and more to enhance your tech skills.


Non penis canina est!