Hướng dẫn nodejs read-last line of file - nodejs đọc dòng cuối cùng của tệp

Tôi đang sử dụng ví dụ này để đọc một tệp lớn:

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

var instream = fs.createReadStream('your/file');
var outstream = new stream;
var rl = readline.createInterface(instream, outstream);

rl.on('line', function(line) {
  // process line here
});

rl.on('close', function() {
  // do something on finish here
});

Và tôi muốn biết khi nào line là dòng tệp cuối cùng. Tôi đọc các tài liệu nhưng không thể tìm thấy một giải pháp. Tôi đã thử:

rl.on('line', function(line) {
    if (line == '' || line == require("os").EOL)
        console.log('eof');        
});

Nhưng nó không hoạt động. Bạn có đề nghị nào không. Cảm ơn vì đã đọc.
Do you have any suggestions. Thanks for reading.

Hỏi ngày 18 tháng 10 năm 2016 lúc 11:48Oct 18, 2016 at 11:48

Hướng dẫn nodejs read-last line of file - nodejs đọc dòng cuối cùng của tệp

1

Vấn đề với câu trả lời của Axel là anh ta giả định dòng cuối cùng của một tệp sẽ trống hoặc EOL. Đây không phải là trường hợp.

// fileTools.js
const fs = require('fs');
const readline = require('readline');
const Stream = require('stream');

exports.getLastLine = (fileName, minLength) => {
    let inStream = fs.createReadStream(fileName);
    let outStream = new Stream;
    return new Promise((resolve, reject)=> {
        let rl = readline.createInterface(inStream, outStream);

        let lastLine = '';
        rl.on('line', function (line) {
            if (line.length >= minLength) {
                lastLine = line;
            }
        });

        rl.on('error', reject)

        rl.on('close', function () {
            resolve(lastLine)
        });
    })
}

Để sử dụng:

const getLastLine = require('./fileTools.js').getLastLine
const fileName = 'C:\\someWinDir\\somelog.log'
const minLineLength = 1
getLastLine(fileName, 1)
    .then((lastLine)=> {
        console.log(lastLine)
    })
    .catch((err)=> {
        console.error(err)
    })

Đã trả lời ngày 21 tháng 9 năm 2017 lúc 17:02Sep 21, 2017 at 17:02

Michael Hobbsmichael HobbsMichael Hobbs

1.6171 Huy hiệu vàng14 Huy hiệu bạc24 Huy hiệu đồng1 gold badge14 silver badges24 bronze badges

4

Tôi đã sử dụng exec(tail -n 1 ) trong bộ kiểm tra mocha của mình vì tôi biết chính xác những gì nó làm và nó thực hiện nó với ít dòng mã hơn ...

Có lẽ nó ít tối ưu hơn một chút khi xây dựng một ứng dụng

const { exec } = require("child_process");
exec("tail -n 1 nginx_access.log",  (error, stdout, stderr) => {
   console.log(stdout)
})

Đã trả lời ngày 27 tháng 11 năm 2020 lúc 10:06Nov 27, 2020 at 10:06

fotoflofotoflofotoflo

6831 Huy hiệu vàng6 Huy hiệu bạc18 Huy hiệu đồng1 gold badge6 silver badges18 bronze badges

Lưu dòng bạn nhận được bên trong một biến toàn cầu, sau đó hiển thị nó khi bạn đến cuối tệp.

var lastLine = '';
rl.on('line', function(line) {
    if (line == '' || line == require("os").EOL) {
        console.log('eof, last line is', lastLine);
        return;
    }

    lastLine = line;
});

Đã trả lời ngày 18 tháng 10 năm 2016 lúc 11:58Oct 18, 2016 at 11:58

Hướng dẫn nodejs read-last line of file - nodejs đọc dòng cuối cùng của tệp

Axel isouardaxel isouardAxel Isouard

1.4781 Huy hiệu vàng24 Huy hiệu bạc38 Huy hiệu đồng1 gold badge24 silver badges38 bronze badges

3

Để đọc N dòng cuối cùng của một tệp lớn một cách hiệu quả, chúng ta có thể sử dụng gói npm này

rl.on('line', function(line) {
    if (line == '' || line == require("os").EOL)
        console.log('eof');        
});
0

https://www.npmjs.com/package/read-last-lines

Ví dụ Đọc 50 dòng cuối cùng của một tệp:

const readLastLines = require('read-last-lines');
readLastLines.read('path/to/file', 50)
    .then((lines) => console.log(lines));

Đã trả lời ngày 26 tháng 12 năm 2020 lúc 8:18Dec 26, 2020 at 8:18

Hướng dẫn nodejs read-last line of file - nodejs đọc dòng cuối cùng của tệp

Spetsnazspetsnazspetsnaz

Huy hiệu Bạc 1411 Huy hiệu Đồng1 silver badge5 bronze badges

1

Lưu dòng hiện tại và khi bạn đi đến cuối tệp, bạn có dòng cuối cùng. Luồng đầu vào phát ra một sự kiện "kết thúc" khi tất cả dữ liệu của nó được tiêu thụ. https://nodejs.org/api/stream.html#stream_event_end

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

var instream = fs.createReadStream('your/file');
var outstream = new stream;
var rl = readline.createInterface(instream, outstream);

var currentLine;

rl.on('line', function(line) {
    currentLine = line;

    // process line here
});

instream.on('end', function() {
    // currentLine is now the last line
    // use currentLine here
});

Bạn có thể sử dụng sự kiện

rl.on('line', function(line) {
    if (line == '' || line == require("os").EOL)
        console.log('eof');        
});
1, nhưng điều đó phát ra vì một vài lý do nữa, như ngắt hoặc gọi
rl.on('line', function(line) {
    if (line == '' || line == require("os").EOL)
        console.log('eof');        
});
2, nhưng những điều đó có thể không ảnh hưởng đến bạn. https://nodejs.org/api/readline.html#readline_event_close

Đã trả lời ngày 25 tháng 5 năm 2019 lúc 23:46May 25, 2019 at 23:46

Hướng dẫn nodejs read-last line of file - nodejs đọc dòng cuối cùng của tệp

BlueblueBlue

5463 Huy hiệu bạc10 Huy hiệu Đồng3 silver badges10 bronze badges

2