0

So I have a text file test.txt with lines similar to:

08/12/2021 
test1
test2
test3
... (some entries)
12/12/2021 
test21
test22
test23
... (some entries)
24/12/2021

What should I to write next in order to filter the text file to get the lines between the two newest dates??

const fs = require('fs');
fs.watchFile('test.txt', (eventType, filename) => {
  fs.readFile('test.txt', 'utf-8', (err, data) => {
      const arr = data.toString().replace(/\r\n/g,'\n').split('\n');
  ...

The output will be something such as:

test21
test22
test23
... (some entries)

Which are the entries between the two newest dates.

Update: The text file is actually constantly writing in entries and will input the current date at the end of the day. Which now I am trying to extract the entries between the previous and newest date for further process

Peanut Jams
  • 374
  • 2
  • 9

3 Answers3

0

I don't know about javascript but it seems you are looking like this one Is there any way to find data between two dates which are presend in same string and store it to json object

Prime
  • 1
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 09 '21 at 08:50
0

You can do one thing, find the indexOf start date and end date then you can slice the contents. You can do something like this:

try {
const data = fs.readFileSync('test.txt', {encoding:'utf8', flag:'r'}),
      start = data.indexOf('start date'),
      end = data.lastIndexOf('end data');

const trimText = data.slice(start, end);

} catch(e) {
  console.log(e)
}

This method will work well for small file if the file is large we need to read it asynchronously and check for the start and end date while reading it.

Apoorva Chikara
  • 8,277
  • 3
  • 20
  • 35
  • Um, the "start & end date" will not actually include in the file, it's just for explaining the question. I update the question, sorry that was misunderstood. – Peanut Jams Dec 09 '21 at 05:49
  • Where are these entires made in a text file or in a directory? I can see there is text.txt file where you are putting the entries. How do you want it? Please elaborate and make things clear in the question. – Apoorva Chikara Dec 09 '21 at 05:51
  • It's done with another program wrote in different language, but now I am trying to find a way to process the file with js. – Peanut Jams Dec 09 '21 at 06:01
  • 1
    It doesn't matter which programming language you used to write to the file. My question is what do you want from that text file. Can you add the steps relevant to the flow you need and it should be clear so I can answer that in one go? – Apoorva Chikara Dec 09 '21 at 06:07
0

const test_data = `08/12/2021
test0
test2
test3
12/12/2021`;

console.log(
  test_data.split(/\d+\/\d+\/\d+\n?/g).slice(1, -1)
);
samuelcolt
  • 253
  • 1
  • 5