通过nodejs比较两条线来提取线条

时间:2018-03-15 09:04:44

标签: node.js file compare line extract

假设我有一个包含以下行的文本文件......

显示

用户
d
Ë
响应
˚F

我想通过nodejs提取数组中的值D和E ...我该如何实现?

1 个答案:

答案 0 :(得分:0)

你有:

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

// we will save the result in this array
const result = []

let afterUser = false

const rl = readline.createInterface(fs.createReadStream('text.txt'))

const listener = rl.on('line', line => {
  // finish the listener if we reach Response line
  if (line === 'Response') {
    rl.close()
    return
  }

  // if we are after User line push the item to the array
  if (afterUser) {
    result.push(line)
  }

  // we'll set afterUser to true if we detect the User line
  if (!afterUser && line === 'User') {
    afterUser = true
  }
})

// after finish reading the file we can show the result
rl.on('close', () => {
  console.log(result)
})