How To Remove Line Breaks And Make Single Line Text?
I have a log file where events are in multiple lines. In order to make event into single line, first I have to separate lines that contain date and lines from those that are withou
Solution 1:
You can do something like this:
const split = require('split');
const regex = /\[\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\w+\]/;
let items = [];
let item = [];
fs.createReadtStream(path.join(dir, logFile), 'utf8')
.pipe(split()).on('data', (line) => {
if (regex.test(line)) {
item = [];
items.push(item);
}
item.push(line);
});
let lines = items.map(item => item.join(' '));
lines.forEach(line =>console.log(line));
You can add a new line to an array every time there is a new line, but when there is a line with a date then you can put that array into another array and create a new array for those single lines. Then you can combine the elements within the inner arrays by joining them and you will have a large array of combined lines - the array called lines
in that example.
Post a Comment for "How To Remove Line Breaks And Make Single Line Text?"