Reputation: 45
How can i find all lines in a file between words or between lines "startString" and "endString"?
const fs = require('fs')
const startString = '-----CompilerOutput:-stderr----------'
const endString = '-----EndCompilerOutput---------------'
const file = fs.readFileSync('./history.log', 'utf8')
let arr = file.split(/\r?\n/);
arr.forEach((line, idx)=> {
if(line.includes(startString, endString)){
console.log((idx+1)+':'+ line);
}
})
Upvotes: 0
Views: 47
Reputation: 35482
You can slice the file:
const startIndex = file.indexOf(startString) + startString.length;
const endIndex = file.indexOf(endString);
const between = file.slice(startIndex, endIndex);
console.log(between);
Upvotes: 1