Click here to Skip to main content
15,077,137 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,
1) I have requirement to save data to a textfile.
2)Then consequently perform a comparison for each line in the text file.
3) If there is line of word that exist on the file report
So far am able to read through the content and display on the console
But I dont know how to go about making the comparison.

Example: Given I ran my code the first time and saved on the textfile

Line #1'apple'
Line #2'mangoes'
Line #3'orange'

On the second > runs I want perform some kind of check or match
Say I am saving thesame :

Line #1'apple'
Line #2'mangoes'
Line #3'orange'

I want to be able to capture these matches.

***** Here is the code I have to read what was already in the file
Please any direction is greatly appreciated.

//***** create fs package
fs = require('fs');
readline = require('readline');
//const fs = require('fs');
//****** fileLoc to save pdf info
fileLoc ="C:/rawDeal.txt"

//********* ('\r\n') in input.txt as a single line break.
require('fs').readFileSync(fileLoc , 'utf-8').split(/\r?\n/).forEach(function(line){

What I have tried:

//*****     create fs package
 fs = require('fs');
 readline = require('readline');
//const fs = require('fs');
//******     fileLoc to save pdf info
 fileLoc ="C:/rawDeal.txt"

  require('fs').readFileSync(fileLoc , 'utf-8').split(/\r?\n/).forEach(function(line){

******** Updated code that does not work*************************************.

const fileLines = fs.readFileSync(file, 'utf-8').split(/\r?\n/)
    for(let index = 0; index < fileLines.length - 1; index++){
        if(fileLines[index] === fileLines[index+1])
          console.log("Matched lines are"+fileLines);
    }else if(fileLines[index] !== fileLines[index+1])
          console.log("Matched lines are"+fileLines);
          console.log("No Matched lines found"+fileLines);
Updated 2-Mar-21 4:02am
Richard MacCutchan 21-Feb-21 4:43am
"But I dont know how to go about making the comparison."
What are you supposed to compare it with?
ekonapikin1990 21-Feb-21 7:56am
I will be comparing historical data.
for example when script runs for the first time it will save data from a given case id
these data are dynamic, persons last name, first name, DOB etc,
When the script runs the next time and saves the next info I just want to make sure none of the details listed above already exist.
If there is a matching I want to fail my test .
Thank you
Richard MacCutchan 2-Mar-21 10:51am
But you are comparing each line with the next one in the file. And you are printing ""Matched lines are"+fileLines" if they match and if they do not match, which is not correct.

It is not clear from your question what you are trying to match against, but it would make more sense to create a function that takes a file and a string, and compare the string with each line of the file. You can then call the function with different values for the string as well as the file.
ekonapikin1990 2-Mar-21 11:42am
Just to be more clearer "I am saving data for a person[fn,ln,dob,income, salary]
Now all these data is tracked with *caseid*
Now this information is geting saved in the same location and I just append to the file everytime.
My ask is say in an event for a give caseid.
caseid;1234 that has person{Jake Anrrera,1978,engineer,120000}
I want to catch any line that has caseid 1234 data.
So if person data already exist then alert.
Richard MacCutchan 2-Mar-21 12:03pm
It really does not matter what data you are referring to, the issue is exactly the same. You start with a search key (caseid etc.), and you need to compare that against each line in the existing file. If you find a match then you can report that and stop the search. If you get to the end of the file without finding a match then you can add the new data to the file.
ekonapikin1990 2-Mar-21 12:14pm
ok thank you!
ekonapikin1990 2-Mar-21 11:32am
Ok thanks for your input sir

1 solution

(pseudo code)

If you want to compare 3 lines, don't split; read the file as a string and do a string compare for:

"apple" + NewLine + "mangoes" + NewLine + "orange" + NewLine

If it's the first 3 lines you want to compare, do a compare with a starting offset of zero.
ekonapikin1990 23-Feb-21 14:52pm
Thanks for your answer sir, it will be great if you can show how to go with some code snipet.
Given am saving my data in 'fileLoc ="C:/rawDeal.txt"
and first data saved was "apple" + NewLine + "mangoes" + NewLine + "orange" + NewLine
For the next runs I want validate if say 'mangoes' exist at a certain line -then report this as pass/fail

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900