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

  • Home
  • SEARCH
  • 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 388423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T15:46:05+00:00 2026-05-12T15:46:05+00:00

Disclaimer: I’m not a programmer, never was, never learned algorithms, CS, etc. Just have

  • 0

Disclaimer: I’m not a programmer, never was, never learned algorithms, CS, etc. Just have to work with it.

My question is: I need to split a huge (over 4 GB) CSV file into smaller ones (then process it with require 'win32ole') based on the first field. In awk it’s rather easy:

awk -F ',' '{myfile=$1 ; print $0 >> (myfile".csv")}' KNAGYFILE.csv

But with ruby I did:

open('hugefile').each { |hline|
    accno = hline[0,12]
    nline = hline[13,10000].gsub(/;/,",")
    accfile = File.open("#{accno.to_s}.csv", "a")
    accfile.puts nline
    accfile.close
}

Then recognized that it’s resource inefficient (several file open/close). I’m sure there’s a better way to do it, could You explain me how?

UPDATE: just forgot to mention, that the file is sorted on the first column. E.g. if this is hugefile:

012345678901,1,1,1,1,1,1
012345678901,1,2,1,1,1,1
012345678901,1,1,A,1,1,1
012345678901,1,1,1,1,A,A
A12345678901,1,1,1,1,1,1
A12345678901,1,1,1,1,1,1
A12345678901,1,1,1,1,1,1
A12345678901,1,1,1,1,1,1

Then I need two new files, named 012345678901.csv and A12345678901.csv.

  • 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-12T15:46:05+00:00Added an answer on May 12, 2026 at 3:46 pm

    Your awk solution will have to open the file just as many times, so I would think you’d get the same resource usage.

    You can keep the file open until $1 changes:

    prev = nil
    File.foreach('hugefile') do |hline|
      accno = hline[0,12]
      nline = hline[13,10000].gsub(/;/,",")
      if prev != accno
        accfile.close rescue nil
        accfile = File.open("#{accno.to_s}.csv", "a")
        prev = accno
      end
      accfile.puts nline
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 212k
  • Answers 212k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer MSDN This is the MSDN link if (!this.Page.ClientScript.IsClientScriptBlockRegistered(typeof(Page), "Utils")) {… May 12, 2026 at 10:18 pm
  • Editorial Team
    Editorial Team added an answer Here's the official API docs for the net package. Regarding… May 12, 2026 at 10:18 pm
  • Editorial Team
    Editorial Team added an answer SELECT q.id, owner.fullname, creator.fullname FROM quotes q LEFT JOIN users… May 12, 2026 at 10:18 pm

Related Questions

Disclaimer: I'm stuck on TFS and I hate it. My source control structure looks
Disclaimer: I'm fairly new to python! If I want all the lines of a
Disclaimer: I'm new to the REST school of thought, and I'm trying to wrap
Disclaimer: I did check other questions that seemed related, but this one is much
Disclaimer: I am an Xcode / iPhone SDK Noob. I am trying to establish

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.