# Compare .txt files

**URL:** <https://discourse.nodered.org/t/compare-txt-files/36654>\
**Category:** General\
**Created:** [28 November 2020 00:08 UTC](https://discourse.nodered.org/t/compare-txt-files/36654 "2020-11-28T00:08:16Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![rand](https://avatars.discourse-cdn.com/v4/letter/r/ebca7d/32.png) [@rand](https://discourse.nodered.org/u/rand)\
**Post date:** [28 November 2020 00:08 UTC](https://discourse.nodered.org/t/compare-txt-files/36654/1 "2020-11-28T00:08:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [28 November 2020 03:11 UTC](https://discourse.nodered.org/t/compare-txt-files/36654/2 "2020-11-28T03:11:33Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [28 November 2020 04:51 UTC](https://discourse.nodered.org/t/compare-txt-files/36654/3 "2020-11-28T04:51:35Z")

</div>

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.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/2/62d26ab9d978b5163244e747e5ab3d4d18b47fe1.png)

**Test Flow :**

```auto
[{"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()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes)

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [28 November 2020 07:25 UTC](https://discourse.nodered.org/t/compare-txt-files/36654/4 "2020-11-28T07:25:39Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rand](https://avatars.discourse-cdn.com/v4/letter/r/ebca7d/32.png) [@rand](https://discourse.nodered.org/u/rand)\
**Post date:** [28 November 2020 22:44 UTC](https://discourse.nodered.org/t/compare-txt-files/36654/5 "2020-11-28T22:44:46Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [28 November 2020 22:53 UTC](https://discourse.nodered.org/t/compare-txt-files/36654/6 "2020-11-28T22:53:24Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [28 November 2020 23:25 UTC](https://discourse.nodered.org/t/compare-txt-files/36654/7 "2020-11-28T23:25:42Z")

</div>

> [@rand](#):
>
> Is there a way I can clear the .txt files manually or automatically after I see the results?

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?](https://askubuntu.com/questions/390167/how-to-clear-text-in-a-file)

**[EDIT]**  
It would be preferable to use node-red's **[Context](https://nodered.org/docs/user-guide/context)** instead of writing and deleting txt files

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [29 November 2020 10:30 UTC](https://discourse.nodered.org/t/compare-txt-files/36654/8 "2020-11-29T10:30:16Z")

</div>

> [@rand](#):
>
> I am reading RFID Chips from a reader and inputting the result to a .txt file.

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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [13 December 2020 10:30 UTC](https://discourse.nodered.org/t/compare-txt-files/36654/9 "2020-12-13T10:30:18Z")

</div>

Closed on 13 December 2020 10:30 UTC

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