Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i diff two files from the web

Tags:

I want to see the differences of 2 files that not in the local filesystem but on the web. So, i think if have to use diff, curl and some kind of piping.

Something like

curl http://to.my/file/one.js http://to.my/file.two.js | diff  

but it doesn't work.

like image 589
poolsideDev Avatar asked May 03 '13 14:05

poolsideDev


People also ask

How can I compare 2 files?

Right-click on the first file. Click on “Select for Compare” from the menu. Proceed to right-click on the second file. Click on “Compare with Selected.

How can I find the difference between two HTML files?

How to compare HTML files. Upload the two HTML files to be compared separately. Press the "COMPARE" button. View the highlighting differences between two HTML files.


2 Answers

The UNIX tool diff can compare two files. If you use the <() expression, you can compare the output of the command within the indirections:

diff <(curl file1) <(curl file2) 

So in your case, you can say:

diff <(curl -s http://to.my/file/one.js) <(curl -s http://to.my/file.two.js) 
like image 94
fedorqui 'SO stop harming' Avatar answered Oct 21 '22 07:10

fedorqui 'SO stop harming'


Some people arriving at this page might be looking for a line-by-line diff rather than a code-diff. If so, and with coreutils, you could use:

comm -23 <(curl http://to.my/file/one.js | sort) \          <(curl http://to.my/file.two.js | sort) 

To get lines in the first file that are not in the second file. You could use comm -13 to get lines in the second file that are not in the first file.

If you're not restricted to coreutils, you could also use sd (stream diff), which doesn't require sorting nor process substitution and supports infinite streams, like so:

curl http://to.my/file/one.js | sd 'curl http://to.my/file.two.js' 

The fact that it supports infinite streams allows for some interesting use cases: you could use it with a curl inside a while(true) loop (assuming the page gives you only "new" results), and sd will timeout the stream after some specified time with no new streamed lines.

Here's a blogpost I wrote about diffing streams on the terminal, which introduces sd.

like image 26
mlg Avatar answered Oct 21 '22 08:10

mlg