I was wondering if it was possible to split a file into equal parts (edit: = all equal except for the last), without breaking the line? Using the split command in Unix, lines may be broken in half. Is there a way to, say, split up a file in 5 equal parts, but have it still only consist of whole lines (it’s no problem if one of the files is a little larger or smaller)? I know I could just calculate the number of lines, but I have to do this for a lot of files in a bash script. Many thanks!
Share
If you mean an equal number of lines,
splithas an option for this:If you need to know what that
75should really be forNequal parts, its:where total lines can be obtained with
wc -l.See the following script for an example:
This outputs:
More recent versions of
splitallow you to specify a number ofCHUNKSwith the-n/--numberoption. You can therefore use something like:(that’s
ell-slash-six, meaninglines, notone-slash-six).That will give you roughly equal files in terms of size, with no mid-line splits.
I mention that last point because it doesn’t give you roughly the same number of lines in each file, more the same number of characters.
So, if you have one 20-character line and 19 1-character lines (twenty lines in total) and split to five files, you most likely won’t get four lines in every file.