programing

node.js에서 한 줄씩 파일을 읽으시겠습니까?

yoursource 2022. 12. 21. 23:07
반응형

node.js에서 한 줄씩 파일을 읽으시겠습니까?

큰 파일을 한 줄씩 읽으려고 합니다.Quora에서 그 주제를 다루는 질문을 찾았지만 모든 것이 잘 맞아떨어지도록 연결고리를 놓쳤다.

 var Lazy=require("lazy");
 new Lazy(process.stdin)
     .lines
     .forEach(
          function(line) { 
              console.log(line.toString()); 
          }
 );
 process.stdin.resume();

이 샘플에서와 같이 STDIN 대신 파일에서 한 줄씩 읽는 방법을 알고 싶습니다.

나는 시도했다.

 fs.open('./VeryBigFile.csv', 'r', '0666', Process);

 function Process(err, fd) {
    if (err) throw err;
    // DO lazy read 
 }

효과가 없어요.PHP와 같은 것을 다시 사용할 수 있다는 것을 알고 있습니다만, 이것을 생각해 보고 싶습니다.

다른 답변은 실행 중인 서버의 메모리 용량보다 파일이 훨씬 더 커서 작동하지 않을 것 같습니다.

Node.js v0.12 및 Node.js v4.0.0 이후로는 안정적인 리드라인 코어 모듈이 있습니다.외부 모듈을 사용하지 않고 파일에서 행을 읽는 가장 쉬운 방법은 다음과 같습니다.

const fs = require('fs');
const readline = require('readline');

async function processLineByLine() {
  const fileStream = fs.createReadStream('input.txt');

  const rl = readline.createInterface({
    input: fileStream,
    crlfDelay: Infinity
  });
  // Note: we use the crlfDelay option to recognize all instances of CR LF
  // ('\r\n') in input.txt as a single line break.

  for await (const line of rl) {
    // Each line in input.txt will be successively available here as `line`.
    console.log(`Line from file: ${line}`);
  }
}

processLineByLine();

또는 다음 중 하나:

var lineReader = require('readline').createInterface({
  input: require('fs').createReadStream('file.in')
});

lineReader.on('line', function (line) {
  console.log('Line from file:', line);
});

은 ( 최종 행이 (Node v0.12 이후).\n.

업데이트: 이 예는 Node API 공식 문서에 추가되었습니다.

이러한 간단한 조작에서는 서드파티 모듈에 의존할 필요가 없습니다.살살해.

var fs = require('fs'),
    readline = require('readline');

var rd = readline.createInterface({
    input: fs.createReadStream('/path/to/file'),
    output: process.stdout,
    console: false
});

rd.on('line', function(line) {
    console.log(line);
});

는 없다open.ReadStream.

fs.createReadStream

다음 이 을 에 합니다.Lazy

2019년 갱신

Nodejs 공식 문서에 이미 훌륭한 예가 게시되어 있습니다.여기

그러기 위해서는 최신 Nodejs가 머신에 설치되어 있어야 합니다.> 11.4

const fs = require('fs');
const readline = require('readline');

async function processLineByLine() {
  const fileStream = fs.createReadStream('input.txt');

  const rl = readline.createInterface({
    input: fileStream,
    crlfDelay: Infinity
  });
  // Note: we use the crlfDelay option to recognize all instances of CR LF
  // ('\r\n') in input.txt as a single line break.

  for await (const line of rl) {
    // Each line in input.txt will be successively available here as `line`.
    console.log(`Line from file: ${line}`);
  }
}

processLineByLine();
require('fs').readFileSync('file.txt', 'utf-8').split(/\r?\n/).forEach(function(line){
  console.log(line);
})

파일을 한 줄씩 읽기 위한 매우 좋은 모듈이 있습니다. 그것은 줄 바꿈이라고 불립니다.

이렇게 쓰면 됩니다.

var lineReader = require('line-reader');

lineReader.eachLine('file.txt', function(line, last) {
  console.log(line);
  // do whatever you want with line...
  if(last){
    // or check if it's the last one
  }
});

더 많은 제어가 필요한 경우 "filename-style" 인터페이스를 사용하여 파일을 반복할 수도 있습니다.

lineReader.open('file.txt', function(reader) {
  if (reader.hasNextLine()) {
    reader.nextLine(function(line) {
      console.log(line);
    });
  }
});

오래된 토픽이지만, 이것은 유효합니다.

var rl = readline.createInterface({
      input : fs.createReadStream('/path/file.txt'),
      output: process.stdout,
      terminal: false
})
rl.on('line',function(line){
     console.log(line) //or parse line
})

간단해 외부 모듈이 필요 없어

언제든지 자신의 라인 리더를 굴릴 수 있습니다.아직 이 스니펫을 벤치마킹하지 않았지만, 들어오는 청크의 스트림을 '\n' 뒤에 붙이지 않고 행으로 올바르게 분할합니다.

var last = "";

process.stdin.on('data', function(chunk) {
    var lines, i;

    lines = (last+chunk).split("\n");
    for(i = 0; i < lines.length - 1; i++) {
        console.log("line: " + lines[i]);
    }
    last = lines[i];
});

process.stdin.on('end', function() {
    console.log("line: " + last);
});

process.stdin.resume();

로그 해석 중에 데이터를 축적할 필요가 있는 빠른 로그 해석 스크립트를 작업할 때 생각해 낸 것입니다.또한 perl이나 bash를 사용하는 대신 js와 노드를 사용하여 이 작업을 시도해 보는 것이 좋다고 생각했습니다.

어쨌든 작은 nodejs 스크립트는 스스로 억제하고 서드파티 모듈에 의존해서는 안 된다고 생각합니다.따라서 이 질문에 대한 답변을 모두 읽고 각각 다양한 모듈을 사용하여 회선 해석을 처리한 후 13개의 SLOC 네이티브노드js 솔루션이 도움이 될 수 있습니다.

캐리어 모듈의 경우:

var carrier = require('carrier');

process.stdin.resume();
carrier.carry(process.stdin, function(line) {
    console.log('got one line: ' + line);
});

노드에서의 드레인/드라이브/드라이브 동작으로 인해 Lazy를 사용하여 이러한 행을 한 줄씩 읽고 다른 스트림에 쓰려고 하면 대량의 메모리 누수가 발생하게 됩니다(http://elegantcode.com/2011/04/06/taking-baby-steps-with-node-js-pumping-data-between-streams/ (이 사람을 사랑합니다) 참조).Lazy를 자세히 살펴보지는 않았지만, 읽기 스트림을 일시 중지하고 Lazy를 종료하지 않고 배수할 수는 없었습니다.

대량의 csv 파일을 xml 문서로 처리하는 코드를 작성했습니다.여기서 코드를 보실 수 있습니다.https://github.com/j03m/node-csv2xml

Lazy 라인으로 이전 리비전을 실행하면 누수가 발생합니다.최신 리비전은 전혀 새지 않기 때문에 리더/프로세서의 베이스로 사용할 수 있을 것입니다.거기에 커스텀 물건이 좀 있긴 하지만요

편집: 필요에 따라 충분히 큰 xml fragment를 쓸 수 있을 때까지 Lazy 코드도 정상적으로 동작하고 있었습니다.작은 덩어리는 괜찮았습니다.

편집:

트랜스폼스트림을 사용합니다.


BufferedReader를 사용하면 행을 읽을 수 있습니다.

new BufferedReader ("lorem ipsum", { encoding: "utf8" })
    .on ("error", function (error){
        console.log ("error: " + error);
    })
    .on ("line", function (line){
        console.log ("line: " + line);
    })
    .on ("end", function (){
        console.log ("EOF");
    })
    .read ();

대부분의 경우 이 정도면 충분합니다.

const fs = require("fs")

fs.readFile('./file', 'utf-8', (err, file) => {
  const lines = file.split('\n')

  for (let line of lines)
    console.log(line)
});

이것에 대한 종합적인 솔루션이 없는 것이 불만스러워서, 독자적인 시도(git/npm)를 실시했습니다.복사 붙여넣은 기능 목록:

  • 인터랙티브한 라인 처리(콜백 기반, 파일 전체를 RAM에 로드하지 않음)
  • 필요에 따라 어레이 내의 모든 행을 반환합니다(상세 모드 또는 원시 모드).
  • 인터랙티브하게 스트리밍을 중단하거나 맵/필터와 같은 처리를 수행합니다.
  • 새로운 회선 표기법 검출(PC/Mac/Linux)
  • 올바른 eof/마지막 라인 처리
  • 멀티바이트 UTF-8 문자의 올바른 처리
  • 바이트 오프셋 및 바이트 길이 정보를 라인 단위로 검색합니다.
  • 회선 기반 또는 바이트 기반 오프셋을 사용한 랜덤 액세스
  • 라인 오프셋 정보를 자동으로 매핑하여 랜덤 액세스 속도 향상
  • 의존 관계가 없음
  • 테스트

NIH? 네가 결정해:-)

원래의 회신을 투고하고 나서, 스플릿은 파일내의 행 판독에 매우 사용하기 쉬운 노드 모듈이며, 옵션 파라미터도 사용할 수 있습니다.

var split = require('split');
fs.createReadStream(file)
    .pipe(split())
    .on('data', function (line) {
      //each chunk now is a seperate line! 
    });

대용량 파일에 대해 테스트한 적이 없습니다.확인되면 알려주세요.

function createLineReader(fileName){
    var EM = require("events").EventEmitter
    var ev = new EM()
    var stream = require("fs").createReadStream(fileName)
    var remainder = null;
    stream.on("data",function(data){
        if(remainder != null){//append newly received data chunk
            var tmp = new Buffer(remainder.length+data.length)
            remainder.copy(tmp)
            data.copy(tmp,remainder.length)
            data = tmp;
        }
        var start = 0;
        for(var i=0; i<data.length; i++){
            if(data[i] == 10){ //\n new line
                var line = data.slice(start,i)
                ev.emit("line", line)
                start = i+1;
            }
        }
        if(start<data.length){
            remainder = data.slice(start);
        }else{
            remainder = null;
        }
    })

    stream.on("end",function(){
        if(null!=remainder) ev.emit("line",remainder)
    })

    return ev
}


//---------main---------------
fileName = process.argv[2]

lineReader = createLineReader(fileName)
lineReader.on("line",function(line){
    console.log(line.toString())
    //console.log("++++++++++++++++++++")
})

저는 이 문제에 대처하고 싶었습니다.기본적으로 Perl에서는 다음과 같은 문제가 발생합니다.

while (<>) {
    process_line($_);
}

저의 사용 사례는 서버가 아닌 스탠드아론 스크립트였기 때문에 동기화는 문제 없었습니다.제 기준은 다음과 같습니다.

  • 많은 프로젝트에서 재사용할 수 있는 최소 동기 코드입니다.
  • 파일 크기 또는 줄 수에 제한이 없습니다.
  • 행의 길이에는 제한이 없습니다.
  • UTF-8에서는 BMP 이외의 문자를 포함한 완전한 Unicode를 처리할 수 있습니다.
  • *nix 및 Windows 라인 엔딩 처리 가능(구식 Mac은 필요 없음).
  • 행에 포함할 행 끝 문자.
  • 끝글자를 포함하거나 포함하지 않고 마지막 줄을 처리할 수 있습니다.
  • node.js 배포에 포함되지 않은 외부 라이브러리를 사용하지 마십시오.

이 프로젝트는 node.js의 낮은 수준의 스크립트 타입 코드를 파악하여 Perl과 같은 다른 스크립트 언어를 대체하기 위한 것입니다.

놀라운 노력과 몇 번의 잘못된 시작 후에 이것이 내가 생각해낸 코드이다.꽤 빠르지만 기대했던 것보다 덜 사소하다: (GitHub에서 forking)

var fs            = require('fs'),
    StringDecoder = require('string_decoder').StringDecoder,
    util          = require('util');

function lineByLine(fd) {
  var blob = '';
  var blobStart = 0;
  var blobEnd = 0;

  var decoder = new StringDecoder('utf8');

  var CHUNK_SIZE = 16384;
  var chunk = new Buffer(CHUNK_SIZE);

  var eolPos = -1;
  var lastChunk = false;

  var moreLines = true;
  var readMore = true;

  // each line
  while (moreLines) {

    readMore = true;
    // append more chunks from the file onto the end of our blob of text until we have an EOL or EOF
    while (readMore) {

      // do we have a whole line? (with LF)
      eolPos = blob.indexOf('\n', blobStart);

      if (eolPos !== -1) {
        blobEnd = eolPos;
        readMore = false;

      // do we have the last line? (no LF)
      } else if (lastChunk) {
        blobEnd = blob.length;
        readMore = false;

      // otherwise read more
      } else {
        var bytesRead = fs.readSync(fd, chunk, 0, CHUNK_SIZE, null);

        lastChunk = bytesRead !== CHUNK_SIZE;

        blob += decoder.write(chunk.slice(0, bytesRead));
      }
    }

    if (blobStart < blob.length) {
      processLine(blob.substring(blobStart, blobEnd + 1));

      blobStart = blobEnd + 1;

      if (blobStart >= CHUNK_SIZE) {
        // blobStart is in characters, CHUNK_SIZE is in octets
        var freeable = blobStart / CHUNK_SIZE;

        // keep blob from growing indefinitely, not as deterministic as I'd like
        blob = blob.substring(CHUNK_SIZE);
        blobStart -= CHUNK_SIZE;
        blobEnd -= CHUNK_SIZE;
      }
    } else {
      moreLines = false;
    }
  }
}

시행착오의 결과였기 때문에 더 정리할 수 있었을지도 모릅니다.

제너레이터 기반 라인 리더: https://github.com/neurosnap/gen-readlines

var fs = require('fs');
var readlines = require('gen-readlines');

fs.open('./file.txt', 'r', function(err, fd) {
  if (err) throw err;
  fs.fstat(fd, function(err, stats) {
    if (err) throw err;

    for (var line of readlines(fd, stats.size)) {
      console.log(line.toString());
    }

  });
});

파일을 한 줄씩 읽고 다른 줄에 쓰는 경우:

var fs = require('fs');
var readline = require('readline');
var Stream = require('stream');

function readFileLineByLine(inputFile, outputFile) {

   var instream = fs.createReadStream(inputFile);
   var outstream = new Stream();
   outstream.readable = true;
   outstream.writable = true;

   var rl = readline.createInterface({
      input: instream,
      output: outstream,
      terminal: false
   });

   rl.on('line', function (line) {
        fs.appendFileSync(outputFile, line + '\n');
   });
};
var fs = require('fs');

function readfile(name,online,onend,encoding) {
    var bufsize = 1024;
    var buffer = new Buffer(bufsize);
    var bufread = 0;
    var fd = fs.openSync(name,'r');
    var position = 0;
    var eof = false;
    var data = "";
    var lines = 0;

    encoding = encoding || "utf8";

    function readbuf() {
        bufread = fs.readSync(fd,buffer,0,bufsize,position);
        position += bufread;
        eof = bufread ? false : true;
        data += buffer.toString(encoding,0,bufread);
    }

    function getLine() {
        var nl = data.indexOf("\r"), hasnl = nl !== -1;
        if (!hasnl && eof) return fs.closeSync(fd), online(data,++lines), onend(lines); 
        if (!hasnl && !eof) readbuf(), nl = data.indexOf("\r"), hasnl = nl !== -1;
        if (!hasnl) return process.nextTick(getLine);
        var line = data.substr(0,nl);
        data = data.substr(nl+1);
        if (data[0] === "\n") data = data.substr(1);
        online(line,++lines);
        process.nextTick(getLine);
    }
    getLine();
}

같은 문제가 있어서 위의 솔루션을 생각해 냈습니다만, 다른 사람의 눈에는 비슷하지만, aSync로 큰 파일을 매우 빠르게 읽을 수 있습니다.

도움이 되었으면 좋겠다

이러한 작업을 수행할 때 자문해야 할 두 가지 질문은 다음과 같습니다.

  1. 이 작업을 수행하는 데 사용되는 메모리의 양은 얼마나 됩니까?
  2. 파일 크기에 따라 메모리 사용량이 급격히 증가하고 있습니까?

방법: 음음음음 solutions solutions solutions solutions solutions solutions 등의 해결 방법require('fs').readFileSync()파일 전체를 메모리에 로드합니다.즉, 작업을 수행하는 데 필요한 메모리 양은 파일 크기와 거의 동일합니다.은 '보다 '보다 더 큰 은 피해야 합니다.50mbs

함수 호출 후에 다음 코드 행을 배치하면 함수가 사용하는 메모리 양을 쉽게 추적할 수 있습니다.

    const used = process.memoryUsage().heapUsed / 1024 / 1024;
    console.log(
      `The script uses approximately ${Math.round(used * 100) / 100} MB`
    );

현재 대용량 파일에서 특정 행을 읽는 가장 좋은 방법은 노드의 리드라인을 사용하는 것입니다.이 문서에는 놀라운 예가 있습니다.

이 기능을 잘 하는 작은 모듈이 있으며 다른 많은 프로젝트에서 사용되고 있습니다.npm readline node v10에는 네이티브 readline 모듈이 있기 때문에 모듈을 linebyline https://www.npmjs.com/package/linebyline으로 다시 게시했습니다.

모듈을 사용하지 않는 경우 기능은 매우 간단합니다.

var fs = require('fs'),
EventEmitter = require('events').EventEmitter,
util = require('util'),
newlines = [
  13, // \r
  10  // \n
];
var readLine = module.exports = function(file, opts) {
if (!(this instanceof readLine)) return new readLine(file);

EventEmitter.call(this);
opts = opts || {};
var self = this,
  line = [],
  lineCount = 0,
  emit = function(line, count) {
    self.emit('line', new Buffer(line).toString(), count);
  };
  this.input = fs.createReadStream(file);
  this.input.on('open', function(fd) {
    self.emit('open', fd);
  })
  .on('data', function(data) {
   for (var i = 0; i < data.length; i++) {
    if (0 <= newlines.indexOf(data[i])) { // Newline char was found.
      lineCount++;
      if (line.length) emit(line, lineCount);
      line = []; // Empty buffer.
     } else {
      line.push(data[i]); // Buffer new line data.
     }
   }
 }).on('error', function(err) {
   self.emit('error', err);
 }).on('end', function() {
  // Emit last line if anything left over since EOF won't trigger it.
  if (line.length){
     lineCount++;
     emit(line, lineCount);
  }
  self.emit('end');
 }).on('close', function() {
   self.emit('close');
 });
};
util.inherits(readLine, EventEmitter);

또 다른 솔루션은 순차 실행 프로그램 nsynj를 통해 로직을 실행하는 것입니다.노드 readline 모듈을 사용하여 파일을 한 줄씩 읽으며 약속이나 재귀는 사용하지 않으므로 큰 파일에서도 오류가 발생하지 않습니다.코드는 다음과 같습니다.

var nsynjs = require('nsynjs');
var textFile = require('./wrappers/nodeReadline').textFile; // this file is part of nsynjs

function process(textFile) {

    var fh = new textFile();
    fh.open('path/to/file');
    var s;
    while (typeof(s = fh.readLine(nsynjsCtx).data) != 'undefined')
        console.log(s);
    fh.close();
}

var ctx = nsynjs.run(process,{},textFile,function () {
    console.log('done');
});

위의 코드는 이 예시를 기반으로 합니다.https://github.com/amaksr/nsynjs/blob/master/examples/node-readline/index.js

이것은, 최신의 「슬라우프」또는 「올인 메모리」가 아닌, 프로그레시브한 파일을 읽어내는 심플한 네이티브 솔루션인 파일입니다.async/await할 때 '는 것을 '자연스럽다'고 readline패키지 또는 비코어 종속성.

let buf = '';
for await ( const chunk of fs.createReadStream('myfile') ) {
    const lines = buf.concat(chunk).split(/\r?\n/);
    buf = lines.pop();
    for( const line of lines ) {
        console.log(line);
    }
}
if(buf.length) console.log(buf);  // last line, if file does not end with newline

은 '부호화'에서할 수 .fs.createReadStream 「」를 사용합니다.chunk.toString(<arg>) 이 입맛에하게 조절해 것이 를를들 、 용용용 ) ) 。.split(/\n+/) { highWaterMark: <chunkSize> }.

마세요.processLine(line), 회선 처리 코드가 됩니다.buf the unfortun 쉽쉽도는ReadStream는 이하지 않기 하는 등의 루프 수 따라서 afaik는 파일 크기 비교와 같은 보다 상세한 트릭이 없으면 루프 내에서 마지막 반복을 검출할 수 없습니다.fs.Stats().bytesRead인 것은, 「아, 아, 아」입니다.buf의 끝이 줄바꿈으로 않는 한)\n이 경우 )는for await루프면 충분합니다.

★ 이벤트가 발생한 비동기 버전을 선호하는 경우 다음과 같이 하십시오.

let buf = '';
fs.createReadStream('myfile')
.on('data', chunk => {
    const lines = buf.concat(chunk).split(/\r?\n/);
    buf = lines.pop();
    for( const line of lines ) {
        console.log(line);
    }
})
.on('end', () => buf.length && console.log(buf) );

★ ★의 Import가 , ★의 Import를 부탁드립니다stream package,의해, decompression:core package" gzip "gzip" "gzip" "gzip" "gzip" "gzip" "gzip" "gzip" "gzip" "gzip" "gzip "gzip" 등의 을 체인으로 할 수 .

const { Writable } = require('stream');
let buf = '';
fs.createReadStream('myfile').pipe(
    new Writable({
        write: (chunk, enc, next) => {
            const lines = buf.concat(chunk).split(/\r?\n/);
            buf = lines.pop();
            for (const line of lines) {
                console.log(line);
            }
            next();
        }
    })
).on('finish', () => buf.length && console.log(buf) );

나는 이것을 사용한다:

function emitLines(stream, re){
    re = re && /\n/;
    var buffer = '';

    stream.on('data', stream_data);
    stream.on('end', stream_end);

    function stream_data(data){
        buffer += data;
        flush();
    }//stream_data

    function stream_end(){
        if(buffer) stream.emmit('line', buffer);
    }//stream_end


    function flush(){
        var re = /\n/;
        var match;
        while(match = re.exec(buffer)){
            var index = match.index + match[0].length;
            stream.emit('line', buffer.substring(0, index));
            buffer = buffer.substring(index);
            re.lastIndex = 0;
        }
    }//flush

}//emitLines

스트림에서 이 기능을 사용하여 발신되는 회선 이벤트를 청취합니다.

gr-

아마 '어디서'를 게 것 같아요.readline 있듯이 "Module" (모듈은 "Module" 입니다.readline는 행 판독이 아닌 명령줄 인터페이스를 지향하는 것으로 보입니다.버퍼링에 대해서도 조금 더 불투명합니다.(스트리밍 회선 지향 리더가 필요한 사용자는 버퍼 크기를 조정할 필요가 있을 것입니다).readline module은 최대 1000행이지만 stats 및 tests에서는 34행입니다.

const EventEmitter = require('events').EventEmitter;
class LineReader extends EventEmitter{
    constructor(f, delim='\n'){
        super();
        this.totalChars = 0;
        this.totalLines = 0;
        this.leftover = '';

        f.on('data', (chunk)=>{
            this.totalChars += chunk.length;
            let lines = chunk.split(delim);
            if (lines.length === 1){
                this.leftover += chunk;
                return;
            }
            lines[0] = this.leftover + lines[0];
            this.leftover = lines[lines.length-1];
            if (this.leftover) lines.pop();
            this.totalLines += lines.length;
            for (let l of lines) this.onLine(l);
        });
        // f.on('error', ()=>{});
        f.on('end', ()=>{console.log('chars', this.totalChars, 'lines', this.totalLines)});
    }
    onLine(l){
        this.emit('line', l);
    }
}
//Command line test
const f = require('fs').createReadStream(process.argv[2], 'utf8');
const delim = process.argv[3];
const lineReader = new LineReader(f, delim);
lineReader.on('line', (line)=> console.log(line));

다음은 통계정보가 없는 19줄의 더 짧은 버전입니다.

class LineReader extends require('events').EventEmitter{
    constructor(f, delim='\n'){
        super();
        this.leftover = '';
        f.on('data', (chunk)=>{
            let lines = chunk.split(delim);
            if (lines.length === 1){
                this.leftover += chunk;
                return;
            }
            lines[0] = this.leftover + lines[0];
            this.leftover = lines[lines.length-1];
            if (this.leftover) 
                lines.pop();
            for (let l of lines)
                this.emit('line', l);
        });
    }
}
const fs = require("fs")

fs.readFile('./file', 'utf-8', (err, data) => {
var innerContent;
    console.log("Asynchronous read: " + data.toString());
    const lines = data.toString().split('\n')
    for (let line of lines)
        innerContent += line + '<br>';


});

일일 회선 처리의 모든 논리를 npm 모듈로 정리합니다.line-kit https://www.npmjs.com/package/line-kit

// example
var count = 0
require('line-kit')(require('fs').createReadStream('/etc/issue'),
                    (line) => { count++; },
                    () => {console.log(`seen ${count} lines`)})

디렉토리가 아닌 것을 확인하고 파일 목록에 포함되지 않은 것을 확인한 후 아래 줄을 코드화합니다.

(function () {
  var fs = require('fs');
  var glob = require('glob-fs')();
  var path = require('path');
  var result = 0;
  var exclude = ['LICENSE',
    path.join('e2e', 'util', 'db-ca', 'someother-file'),
    path.join('src', 'favicon.ico')];
  var files = [];
  files = glob.readdirSync('**');

  var allFiles = [];

  var patternString = [
    'trade',
    'order',
    'market',
    'securities'
  ];

  files.map((file) => {
    try {
      if (!fs.lstatSync(file).isDirectory() && exclude.indexOf(file) === -1) {
        fs.readFileSync(file).toString().split(/\r?\n/).forEach(function(line){
          patternString.map((pattern) => {
            if (line.indexOf(pattern) !== -1) {
              console.log(file + ' contain `' + pattern + '` in in line "' + line +'";');
              result = 1;
            }
          });
        });
      }
    } catch (e) {
      console.log('Error:', e.stack);
    }
  });
  process.exit(result);

})();

위의 답변은 모두 서드파티 라이브러리를 사용하여 해결했습니다.Node API에 간단한 솔루션이 포함되어 있습니다.

const fs= require('fs')

let stream = fs.createReadStream('<filename>', { autoClose: true })

stream.on('data', chunk => {
    let row = chunk.toString('ascii')
}))

언급URL : https://stackoverflow.com/questions/6156501/read-a-file-one-line-at-a-time-in-node-js

반응형