Fork me on GitHub

Project Notes

Split Files

Split a text file into parts with simple shell utilities.

Notes

Split By Row Count

Given a text file with many rows, split it into multiple smaller files n lines in length

Simple solution is to use the split utility e.g.

cd example1
split -l 100 ../countries.csv part-

This produces three files with a maximum of 100 lines in each file:

$ wc -l *
     100 part-aa
     100 part-ab
      49 part-ac
     249 total

However this was a CSV file, so it contains a header that might be good to strip first. The split accepts piped data, so can first trim the file with tail:

cd example2
tail -n +2 ../countries.csv | split -l 100 - part-
wc -l *
     100 part-aa
     100 part-ab
      48 part-ac
     248 total

Credits and References

About LCK#242 bashsplittail
Project Source on GitHub Return to the Project Catalog

This page is a web-friendly rendering of my project notes shared in the LittleCodingKata GitHub repository.

LittleCodingKata is my collection of programming exercises, research and code toys broadly spanning things that relate to programming and software development (languages, frameworks and tools).

These range from the trivial to the complex and serious. Many are inspired by existing work and I'll note credits and references where applicable. The focus is quite scattered, as I variously work on things new and important in the moment, or go back to revisit things from the past.

This is primarily a personal collection for my own edification and learning, but anyone who stumbles by is welcome to borrow, steal or reference the work here. And if you spot errors or issues I'd really appreciate some feedback - create an issue, send me an email or even send a pull-request.