1.0.0 • Published 7 years ago
read-last v1.0.0
读取文件最后的内容
获得文件最后N行的内容。
使用
假设读取如下文件:
2012.3.4 19:38 user1 call api test.
2012.3.4 19:39 user4 call api test.
2012.3.4 19:40 user9 call api test.
2012.3.4 19:41 user6 call api test.2012.3.4 19:42 user1 call api test.
2012.3.4 19:43 user2 call api test.2012.3.4 19:44 user1 call api test.
2012.3.4 19:45 user3 call api test.2012.3.4 19:46 user1 call api test.2012.3.4 19:30 user1 call api test.
const readLast = require('read-last')
let filepath = ''//your file path
let lines = 3
readLast(filepath, lines).then(function(msg){
// got it
/*
2012.3.4 19:41 user6 call api test.2012.3.4 19:42 user1 call api test.
2012.3.4 19:43 user2 call api test.2012.3.4 19:44 user1 call api test.
2012.3.4 19:45 user3 call api test.2012.3.4 19:46 user1 call api test.2012.3.4 19:30 user1 call api test.
*/
}, function (err){
// file not exist or typeerror
})
文件的行分隔符通常是\n
或\r
,你可以传入第三个参数重新指定以什么作为行分割符。
readLast(filepath, lines, /\./).then(function(msg){
// got it
/*
.
2012.3.4 19:45 user3 call api test.2012.3.4 19:46 user1 call api test.2012.3.4 19:30 user1 call api test.
*/
}, function (err){
// file not exist or typeerror
})
readLast(filepath, lines, 't').then(function(msg){
// got it
/*
t.2012.3.4 19:30 user1 call api test.
*/
}, function (err){
// file not exist or typeerror
})
readLast(filepath, lines, ['t','r']).then(function(msg){
// got it
/*
r1 call api test.
*/
}, function (err){
// file not exist or typeerror
})
注意,第三个参数只能是单个字符的字符串,或单个字符为元素的数组,或匹配单字符的正则表达式。
如'a'
,['a','b','c']
,/a|b|c/
否则会获取到整个文件。
1.0.0
7 years ago