gpt4 book ai didi

node.js - 使用 nodejs 从文件中删除最后 n 行

转载 作者:搜寻专家 更新时间:2023-10-31 23:30:28 28 4
gpt4 key购买 nike

我正在尝试使用 fs 作为 nodejs 的一部分从文件中删除最后 3 行。我目前正在将文件读入内存,然后在没有 3 行的情况下再次写入它,但我确信有一种更有效的方法不需要将整个文件读入内存。

我现在的代码

fs.readFile(filename, function (err, data) {
if (err) throw err;
theFile = data.toString().split("\n");
theFile.splice(-3, 3);
fs.writeFile(filename, theFile.join("\n"), function (err) {
if (err) {
return console.log(err);
}
console.log("Removed last 3 lines");
console.log(theFile.length);

});
});

最佳答案

让我们创建一个大文件:

$ base64 /dev/urandom | head -1000000 > /tmp/crap
$ wc -l /tmp/crap
1000000 /tmp/crap
$ du -sh /tmp/crap
74M /tmp/crap

这是您的代码:

$ cat /tmp/a.js
var fs = require('fs');

var filename = '/tmp/crap1';

fs.readFile(filename, function(err, data) {
if(err) throw err;
theFile = data.toString().split("\n");
theFile.splice(-3,3);
fs.writeFile(filename, theFile.join("\n"), function(err) {
if(err) {
return console.log(err);
}
console.log("Removed last 3 lines");
console.log(theFile.length);
});
});

这是我的:

$ cat /tmp/b.js
var fs = require('fs'),
util = require('util'),
cp = require('child_process');

var filename = '/tmp/crap2';
var lines2nuke = 3;
var command = util.format('tail -n %d %s', lines2nuke, filename);

cp.exec(command, (err, stdout, stderr) => {
if (err) throw err;
var to_vanquish = stdout.length;
fs.stat(filename, (err, stats) => {
if (err) throw err;
fs.truncate(filename, stats.size - to_vanquish, (err) => {
if (err) throw err;
console.log('File truncated!');
})
});
});

让我们复制同一个文件:

$ cp /tmp/crap /tmp/crap1
$ cp /tmp/crap /tmp/crap2

让我们看看谁更快:

$ time node a.js
Removed last 3 lines
999998
node a.js 0.53s user 0.19s system 99% cpu 0.720 total

$ time node b.js
File truncated!
node b.js 0.08s user 0.01s system 100% cpu 0.091 total

当我将文件大小增加 10 倍时,我的系统用 a.js 耗尽了内存;但是对于 b.js,它需要:

$ time node b.js
File truncated!
node b.js 0.07s user 0.03s system 6% cpu 1.542 total

我的代码使用 tail ,它不会读取整个文件,它会查找到末尾,然后向后读取 block ,直到达到预期的行数,然后它会以正确的方向显示行,直到文件末尾。现在我现在要消失的字节数。然后我使用 fs.stat ,它告诉我文件中的总字节数。现在,在删除最后的 n 行之后,我知道文件末尾实际需要多少字节。最后,我使用 fs.truncate ,这会导致常规文件被截断为指定的大小(以字节为单位)。

更新:

OP 说平台是 Windows。在那种情况下,我们可以修改此程序,使其调用另一个实用程序,而是在 Node 本身中执行所有操作。幸运的是,所需的功能已经作为 Node 模块提供给我们 read-last-lines .现在更新后的、与操作系统无关的代码如下所示:

$ npm install read-last-lines
$ cat /tmp/c.js
var fs = require('fs'),
rll = require('read-last-lines');

var filename = '/tmp/crap2';
var lines2nuke = 3;

rll.read(filename, lines2nuke).then((lines) => {
var to_vanquish = lines.length;
fs.stat(filename, (err, stats) => {
if (err) throw err;
fs.truncate(filename, stats.size - to_vanquish, (err) => {
if (err) throw err;
console.log('File truncated!');
})
});
});

在 10 倍大小的文件上,它花费了:

$ time node c.js
File truncated!
node c.js 0.14s user 0.04s system 8% cpu 2.022 total

关于node.js - 使用 nodejs 从文件中删除最后 n 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42466149/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com