Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6714891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:32:55+00:00 2026-05-26T08:32:55+00:00

I was wondering if it was possible to split a file into equal parts

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T08:32:55+00:00Added an answer on May 26, 2026 at 8:32 am

    If you mean an equal number of lines, split has an option for this:

    split --lines=75
    

    If you need to know what that 75 should really be for N equal parts, its:

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

    where total lines can be obtained with wc -l.

    See the following script for an 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 the 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 you to specify a number of CHUNKS with the -n/--number option. You can therefore use something like:

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

    (that’s ell-slash-six, meaning lines, not one-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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm wondering if it is possible to split a node into two different colours.
I'm wondering if it's possible for .Net's String.Format() to split an integer apart into
I was wondering if its possible to inject a thread into a remote app
I was wondering is it possible to upload a very big file (over 60MB
I was wondering if it was possible to split a string on whitespace, and
I'm just wondering, is it possible to split up a 'li' element? I want
I was wondering ( if possible ) if there was a program/tool/utility that when
Just toying with the SDK and I was wondering if possible a UITouch event
I'm wondering if its possible to add new class data members at run-time in
I am wondering how its possible to permanently show the vertical bar of a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.