Sri Kadimisetty
Sri Kadimisetty

Reputation: 1982

How to use nodejs to separate contents of file divided by double newline?

What Im trying to do: 1. Read the file 2. Split the read file.

Here is a sample input file -

#This is a comment

Line one of first block,
Line two of first block,
Line three of first block.


Line one of second block.
Line two of second block
line three of second block.

Line one of third block.

Each block is separated by newlines ( no control over whether they would be windows/unix). How to go about using getting each block read separately using nodejs?

Upvotes: 0

Views: 343

Answers (2)

f1nn
f1nn

Reputation: 7047

I'd suggest you to use split() function.

var blocks = str.split([separator],[limit]);

After this you may try to iterate the array like

blocks.each(function(err, block) {
    console.log(block);
});

However, you should check, what to specify as separator - Windows or UNIX (CRLF or just LF).

Upvotes: 1

Anoop
Anoop

Reputation: 23208

You can use JavaScript split to do that. same code in jsfiddle. Will work even there is white space between new line character.

 // assuming content is your text.
    var contentArray = content.split(/\n\s{0,}\n/);

Upvotes: 1

Related Questions