文件数据过滤,并将过滤后的数据存入新的文件。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
const fs = require('fs');
const readline = require('readline');
const path1 = __dirname + '/aid_imei_mac.log';
const path2 = __dirname + '/aid_imei_mac1.log';
let wf = fs.createWriteStream(path2) // 创建一个写入流
const strem = process.stderr; // 终端输出
const rl = readline.createInterface({
input: fs.createReadStream(path1),
// output: process.stdout
});
let i = 0
rl.on('line', (line) => {
if (line.split(',').filter(i => !!i).length == 3) {
wf.write(line + '\n')
i++
// 控制台输入写入进度
strem.cursorTo(0)
strem.write(`写入${i}${(wf.bytesWritten / 1024 / 1024).toFixed(2)}M 数据`)
}
});
rl.on('close', function () {
//将操作系统缓存区中的数据全部写入文件
wf.end(function () {
console.log('\n文件全部写入完毕');
console.log(`共写入${(wf.bytesWritten / 1024 / 1024).toFixed(2)}M 数据`);
});
});
—-publish by CEditor