【问题标题】:How do i diff two files from the web我如何区分来自网络的两个文件
【发布时间】:2013-04-28 00:30:32
【问题描述】:

我想查看不在本地文件系统中但在网络上的 2 个文件的差异。所以,我认为如果必须使用diffcurl 和某种管道。

有点像

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

但它不起作用。

【问题讨论】:

  • 将它们写入本地文件系统? :)

标签: unix curl pipe diff command-line-interface


【解决方案1】:

UNIX 工具diff 可以比较两个文件。如果您使用<() 表达式,您可以比较间接内命令的输出:

diff <(curl file1) <(curl file2)

所以在你的情况下,你可以说:

diff <(curl -s http://to.my/file/one.js) <(curl -s http://to.my/file.two.js)

【讨论】:

  • 不错,我会添加 -s 以使 curl 静音并仅比较内容,例如:diff &lt;(curl -s http://to.my/file/one.js) &lt;(curl -s http://to.my/file.two.js)
【解决方案2】:

某些访问此页面的人可能正在寻找逐行差异而不是代码差异。如果是这样,并且使用 coreutils,您可以使用:

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

获取第一个文件中不在第二个文件中的行。您可以使用comm -13 来获取第二个文件中不在第一个文件中的行。

如果您不限于 coreutils,您还可以使用 sd (stream diff),它不需要排序也不需要处理替换,并且支持无限流,如下所示:

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

它支持无限流的事实允许一些有趣的用例:您可以在 while(true) 循环内使用 curl(假设页面只为您提供“新”结果),sd 将超时在指定时间后流式传输,没有新的流式传输行。

这是一个blogpost我写的关于在终端上区分流,它介绍了sd

【讨论】:

    猜你喜欢
    • 2019-07-07
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 2011-05-05
    • 1970-01-01
    • 2020-09-06
    • 2023-03-18
    相关资源
    最近更新 更多