bash - How to split a file into equal parts, without breaking individual lines? -


this question has answer here:

i wondering if possible split file equal parts (edit: = equal except last), without breaking line? using split command in unix, lines may broken in half. there way to, say, split file in 5 equal parts, have still consist of whole lines (it's no problem if 1 of files little larger or smaller)? know calculate number of lines, have lot of files in bash script. many thanks!

if mean equal number of lines, split has option this:

split --lines=75 

if need know 75 should n equal parts, its:

lines_per_part = int(total_lines + n - 1) / n 

where total lines can obtained wc -l.

see following script example:

#!/usr/bin/bash  # configuration stuff  fspec=qq.c num_files=6  # work out lines per file.  total_lines=$(wc -l <${fspec}) ((lines_per_file = (total_lines + num_files - 1) / num_files))  # split actual file, maintaining lines.  split --lines=${lines_per_file} ${fspec} xyzzy.  # debug information  echo "total lines     = ${total_lines}" echo "lines  per file = ${lines_per_file}"     wc -l xyzzy.* 

this outputs:

total lines     = 70 lines  per file = 12   12 xyzzy.aa   12 xyzzy.ab   12 xyzzy.ac   12 xyzzy.ad   12 xyzzy.ae   10 xyzzy.af   70 total 

more recent versions of split allow specify number of chunks -n/--number option. can therefore use like:

split --number=l/6 ${fspec} xyzzy. 

(that's ell-slash-six, meaning lines, not one-slash-six).

that give equal files in terms of size, no mid-line splits.

i mention last point because doesn't give same number of lines in each file, more same number of characters.

so, if have 1 20-character line , 19 1-character lines (twenty lines in total) , split 5 files, won't 4 lines in every file.


Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -