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 9021369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:14:24+00:00 2026-06-16T05:14:24+00:00

I have a 60GB csv file I need to make some modifications to. The

  • 0

I have a 60GB csv file I need to make some modifications to. The customer wants some changes to the files data, but I don’t want to regenerate the data in that file because it took 4 days to do.

How can I read the file, line by line (not loading it all into memory!), and make edits to those lines as I go, replacing certain values etc.?

  • 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-06-16T05:14:25+00:00Added an answer on June 16, 2026 at 5:14 am

    The process would be something like this:

    1. Open a StreamWriter to a temporary file.
    2. Open a StreamReader to the target file.
    3. For each line:
      1. Split the text into columns based on a delimiter.
      2. Check the columns for the values you want to replace, and replace them.
      3. Join the column values back together using your delimiter.
      4. Write the line to the temporary file.
    4. When you are finished, delete the target file, and move the temporary file to the target file path.

    Note regarding Steps 2 and 3.1: If you are confident in the structure of your file and it is simple enough, you can do all this out of the box as described (I’ll include a sample in a moment). However, there are factors in a CSV file that may need attention (such as recognizing when a delimiter is being used literally in a column value). You can drudge through this yourself, or try an existing solution.


    Basic example just using StreamReader and StreamWriter:

    var sourcePath = @"C:\data.csv";
    var delimiter = ",";
    var firstLineContainsHeaders = true;
    var tempPath = Path.GetTempFileName();
    var lineNumber = 0;
    
    var splitExpression = new Regex(@"(" + delimiter + @")(?=(?:[^""]|""[^""]*"")*$)");
    
    using (var writer = new StreamWriter(tempPath))
    using (var reader = new StreamReader(sourcePath))
    {
        string line = null;
        string[] headers = null;
        if (firstLineContainsHeaders)
        {
            line = reader.ReadLine();
            lineNumber++;
    
            if (string.IsNullOrEmpty(line)) return; // file is empty;
    
            headers = splitExpression.Split(line).Where(s => s != delimiter).ToArray();
    
            writer.WriteLine(line); // write the original header to the temp file.
        }
    
        while ((line = reader.ReadLine()) != null)
        {
            lineNumber++;
    
            var columns = splitExpression.Split(line).Where(s => s != delimiter).ToArray();
    
            // if there are no headers, do a simple sanity check to make sure you always have the same number of columns in a line
            if (headers == null) headers = new string[columns.Length];
    
            if (columns.Length != headers.Length) throw new InvalidOperationException(string.Format("Line {0} is missing one or more columns.", lineNumber));
    
            // TODO: search and replace in columns
            // example: replace 'v' in the first column with '\/': if (columns[0].Contains("v")) columns[0] = columns[0].Replace("v", @"\/");
    
            writer.WriteLine(string.Join(delimiter, columns));
        }
    
    }
    
    File.Delete(sourcePath);
    File.Move(tempPath, sourcePath);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Background: We have large flat files span around 60GB and are inserting into database.
I have some program that generates a lot of data, to be specific encrypting
I have tens of files that are 256MB and total in 40GB - These
Background I have a Spring batch program that reads a file (example file I
have been using no DOCTYPE but rather simply starting with <html> as per HTML5
Have I in some way delete this Task or it is self-destroyed? Task.Factory.StartNew(() =>
Have a form here with bunch of input text fields and a file upload
Have two tables say ABC and XYZ and contain one column which data will
Lets say, I have a Solr Index of 60GB. I want to merge with
Have a problem that seems easy on paper but i'm having a big problem

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.