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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:22:54+00:00 2026-06-09T15:22:54+00:00

I tried to use Node.js to process a 500MB Apache log file, converting its

  • 0

I tried to use Node.js to process a 500MB Apache log file, converting its syntax from

ip.ip.ip.ip - - [02/Aug/2012:05:01:17 -0600] "GET /path/of/access/ HTTP/1.1" 302 26

to

ip.ip.ip.ip - - 02/Aug/2012:05:01:17 GET /path/of/access/ HTTP/1.1 302 26

, then write to another text file.

For better memory control and performance, I used fs.createReadStream and fs.createWriteStream, but only managed to write the first line into output.txt, because the script ends with an error:

{ [Error: EBADF, write] errno: 9, code: 'EBADF' }

Here I posted some info that may help debug.

Head of input.txt:

ip.ip.ip.ip - - [02/Aug/2012:05:01:17 -0600] "GET /path/of/access/ HTTP/1.1" 302 26
ip.ip.ip.ip - - [02/Aug/2012:05:01:17 -0600] "GET /path/of/access/ HTTP/1.1" 302 26
ip.ip.ip.ip - - [02/Aug/2012:05:01:17 -0600] "GET /path/of/access/ HTTP/1.1" 302 26
ip.ip.ip.ip - - [02/Aug/2012:05:01:17 -0600] "GET /path/of/access/ HTTP/1.1" 302 26
ip.ip.ip.ip - - [02/Aug/2012:05:01:17 -0600] "GET /path/of/access/ HTTP/1.1" 302 26
ip.ip.ip.ip - - [02/Aug/2012:05:01:17 -0600] "GET /path/of/access/ HTTP/1.1" 302 26
ip.ip.ip.ip - - [02/Aug/2012:05:01:18 -0600] "GET /path/of/access/ HTTP/1.1" 302 26

Content of output.txt:

ip.ip.ip.ip - - [02/Aug/2012:05:01:17 -0600] "GET /path/of/access/ HTTP/1.1" 302 26

The whole script:

var fs = require('fs');
var data ='';
var n=0;                    //For line control
var r = fs.createReadStream('./input.txt',{
    encoding: 'ascii',
    start:0,
    // end: 100000,
});
var w = fs.createWriteStream('./output.txt',{
    encoding:'ascii'
});
function put(line){         //write into w;
    ++n;
    w.write(line+'\n');
}
function end(){
    r.destroy();
    w.destroy();
}
function onData(chunk){
    var hasNewline = chunk.indexOf('\n')!==-1;
    if(hasNewline){
        var arr = chunk.split('\n');
        var first = arr.shift();
        var last = arr.pop();
        data+=first;
        put(data);          //write a complete line
        arr.forEach(function(line){
            put(line);      //write a complete line
        });
        data=last;
    }else{
        data+=chunk;
    }
    if(n>100){
        end();
    }
}
function onErr(e){
    console.log(e);
}

r.addListener( "data", onData);
r.addListener( "end", end);
r.addListener('error',onErr);
w.addListener('error',onErr);
  • 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-09T15:22:55+00:00Added an answer on June 9, 2026 at 3:22 pm

    You’ve got two issues that I can see.

    The first is that your end function calls destroy on the ReadStream, but in the general case this is triggered from the end event, which means that the stream is already closing, and it is going to call destroy automatically. That means that r.destroy is going to be called twice, triggering an error. This is the cause of the error you are seeing printed.

    The second issue is that you are calling destroy on the WriteStream. I suggest you go read the docs for that: http://nodejs.org/api/stream.html#stream_stream_destroy_1

    Specifically Any queued write data will not be sent, which is why you are missing some of your output.

    Basically, you should ONLY call destroy on the ReadStream if you want it to close early, like in your n > 100 case. Then you want to use WriteStream’s end instead, so the stream has time to write all of the buffered data.

    Here is a simplified version, which I think should work the same. I’d also not bother binding error since errors are automatically printed to the console anyway.

    var fs = require('fs');
    var data ='';
    var n=0;                    //For line control
    
    var r = fs.createReadStream('./input.txt',{
        encoding: 'ascii',
        start:0,
        // end: 100000,
    });
    
    var w = fs.createWriteStream('./output.txt',{
        encoding:'ascii'
    });
    
    r.addListener( "data", function(chunk){
        data += chunk;
        var lines = data.split('\n');
        data = lines.pop();
    
        lines.forEach(function(line){
          if (!r.readable) return; // If already destroyed
          if (n >= 100) {
              // Stop any more 'data' events and close the file.
              // This will also trigger 'close' below and close the writestream.
              r.destroy();
              return;
          }
    
          n++;
          w.write(line + '\n');
        });
    });
    r.addListener( "end", function(){
        // When we hit the end of the file, close the write stream,
        // and write any remaining line content
        w.write(data);
    });
    r.addListener("close", function(){
      w.end();
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I tried to use Hibernate Envers to log the entity histroy. but failed. My
I tried to use this snippet from the Soundcloud API: <script src=http://connect.soundcloud.com/sdk.js> <script> SC.initialize({
I tried to use an image file in my HTML template. But when I
I would like to use MySQL with Node.js on Windows. I have tried to
I'm have a js file that I'm trying to use in a node.js app.
I have tried to use ntwitter node.js module.I have read all the instructions and
I tried to use Cleaver, that is PhoneGap / Cordova as a component of
I tried to use the following function in order to set the div's position
I tried to use AsyncProcessor in test scenario, expecting only one message to run
Im tried to use NAudio demo app for MP3 Streaming in Csharp, when i

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.