Compare .txt files

Hi, I want to compare 2 .txt file components (so compare the words in the file not if something is missing from a line) every 10 minutes and have the output of the difference displayed to me once the 10 minutes is up. How would I be able to do this in the simplest way?

Note: I am a Node-RED beginner. All help, steps, tutorials, will be gladly appreciated!

Example of my scenario:
textone.txt
Cart 3
Cart 2
Cart 1

texttwo.txt
Cart 1
Cart 2

Output after 10 minutes:
Cart 3

Hi and welcome.

That is tricky for me.

But a couple of things which need to be clarified:

How big are these two files? a few lines, or .... lots of lines?

Are you wanting to go down the road of using nodes as much as possible, or are you prepared to jump in feet first and seriously do what ever is needed?

Hi Rand,

My approach would be to read the two files and store each line in arrays (arr1 and arr2)
Then compare in a function node whether the elements/values of arr1 are included in arr2 and return the missing elements

A combination of Nodes are needed in order for the values to reach the Function node as a single message.

Test Flow :

[{"id":"bde9ac92.1486a8","type":"inject","z":"c95d3cba.8bb268","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":140,"y":100,"wires":[["cee63a33.be22e8"]]},{"id":"cee63a33.be22e8","type":"file in","z":"c95d3cba.8bb268","name":"File1","filename":"/home/pi/Desktop/textone.txt","format":"lines","chunk":false,"sendError":false,"encoding":"none","x":250,"y":200,"wires":[["6e3ab76c.42ae4"]]},{"id":"ac2355eb.4ee788","type":"debug","z":"c95d3cba.8bb268","name":"1","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":730,"y":140,"wires":[]},{"id":"6e3ab76c.42ae4","type":"join","z":"c95d3cba.8bb268","name":"","mode":"custom","build":"array","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"num","reduceFixup":"","x":410,"y":200,"wires":[["facf4be6.69a75","cb431c62.669a"]]},{"id":"facf4be6.69a75","type":"file in","z":"c95d3cba.8bb268","name":"File2","filename":"/home/pi/Desktop/texttwo.txt","format":"lines","chunk":false,"sendError":false,"encoding":"none","x":250,"y":340,"wires":[["46c45939.74a4b8"]]},{"id":"46c45939.74a4b8","type":"join","z":"c95d3cba.8bb268","name":"","mode":"custom","build":"array","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"num","reduceFixup":"","x":410,"y":340,"wires":[["e72d72aa.5859b8"]]},{"id":"62d5a6e6.02c198","type":"debug","z":"c95d3cba.8bb268","name":"2","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":750,"y":400,"wires":[]},{"id":"cb431c62.669a","type":"change","z":"c95d3cba.8bb268","name":"","rules":[{"t":"move","p":"payload","pt":"msg","to":"payload.arr1","tot":"msg"},{"t":"set","p":"topic","pt":"msg","to":"arr1","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":600,"y":200,"wires":[["ac2355eb.4ee788","4fe84452.cfcbdc"]]},{"id":"e72d72aa.5859b8","type":"change","z":"c95d3cba.8bb268","name":"","rules":[{"t":"move","p":"payload","pt":"msg","to":"payload.arr2","tot":"msg"},{"t":"set","p":"topic","pt":"msg","to":"arr2","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":600,"y":340,"wires":[["62d5a6e6.02c198","4fe84452.cfcbdc"]]},{"id":"4fe84452.cfcbdc","type":"join","z":"c95d3cba.8bb268","name":"","mode":"custom","build":"merged","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"num","reduceFixup":"","x":790,"y":260,"wires":[["43d5c154.accb4","30086994.f843ce"]]},{"id":"43d5c154.accb4","type":"debug","z":"c95d3cba.8bb268","name":"3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":910,"y":220,"wires":[]},{"id":"30086994.f843ce","type":"function","z":"c95d3cba.8bb268","name":"","func":"let arr1 = msg.payload.arr1\nlet arr2 = msg.payload.arr2\nlet missing = []\n\narr1.forEach(el => {\n    if (!arr2.includes(el)) missing.push(el)\n\n})\n\nmsg.payload = missing;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":960,"y":320,"wires":[["b825784d.dcceb"]]},{"id":"b825784d.dcceb","type":"debug","z":"c95d3cba.8bb268","name":"4","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1100,"y":320,"wires":[]}]

PS1. im on a raspberry Pi so the path to your files need to be changed
PS2 in the example im checking file1 values with file2 .. would the reverse be needed ?

Array.includes()

1 Like

Are you really sure this is what you need to do? The question suggests to me that you are trying to solve a larger problem and have come up with the idea of writing to files and then comparing them, but I wonder whether there may be a better solution to the larger problem. Perhaps you could provide more details of what is going on in case there is a better solution.

3 Likes

Hi @UnborN, thank you so much!
Is there a way I can clear the .txt files manually or automatically after I see the results?

Also, thank you @Trying_to_learn & @Colin!

My .txt files were going to contain less than 10 lines.

The larger problem incorporates other programs like Arduino IDE & CoolTerms. I am reading RFID Chips from a reader and inputting the result to a .txt file. There’ll be two locations I’m trying to compare hence why I’m comparing two .txt files. I want to know the difference between them so I’ll know what’s missing in an area where carts should be. And after I get the result, I want to be able to reset the files.

The first thing I would do then is see if both files have the same number of lines.

Although in retrospect: Do a "word count" on the files.

Then you know if there is a difference in the word count.

That is the wc command in ubuntu - I believe.

Do that on file1 and file2. Compare the two numbers.
Act on that.
If they are the same or different.

But of course that is not really good if they have the same count of words, but the words are different.

In ubuntu you have the diff command.

Simply do a shell command to compare them.
If they don't match, do something.

Would this suffice?

What operating system is node-red on ?
In Unix there's an article with some commands that you can try to run with the Exec node

How to clear text in a file?

[EDIT]
It would be preferable to use node-red's Context instead of writing and deleting txt files

Is the file writing being done in node red or from one of the other devices?
If in node-red then using context would be an easier way to do it.
If from one of the other devices it might be better to switch to using MQTT if possible and then using the context for saving the data.
Using files will always be fraught with problems if you have multiple processes writing to the file at, potentially, the same time.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.