【问题标题】:unix - diff command to output in single-line-per-difference formatunix - diff 命令以单行/差异格式输出
【发布时间】:2016-10-21 05:04:48
【问题描述】:

我的 cygwin 中的 diff 版本有许多高级选项,可以让我每行打印一个差异。

给定两个文件 one.txt 和 two.txt。

一个.txt:

one
two
three
four
five
six

两个.txt

one
two2
three
four
five5
six

并使用以下选项/参数在 cygwin 中运行 diff:

diff -y --suppress-common-lines one.txt two.txt

给出以下输出:

two   |two2
five  |five5

这是我所追求的格式类型,每行打印一个差异。 在我的 dev solaris 机器上,不支持“-y”选项,所以我遇到了如下所示的输出:

2c2
< two
---
> two2
5c5
< five
---
> five5

有谁知道我可以在这个 solaris 盒子上获得每行一个差异的输出的方法吗?也许使用 sed/awk one liner 来按摩这个更原始的差异输出的输出? (请注意,我无法在此 Solaris 机器上安装更新的 diff 版本)。

谢谢!

【问题讨论】:

    标签: unix diff


    【解决方案1】:

    使用 GNU 差异。

    http://www.gnu.org/software/diffutils/

    您可以将其构建并安装到本地目录中,不是吗?如果你有一个主目录、一个编译器和一个 make,你就可以构建你自己的 GNU diff。

    我没有 Solaris,但我无法想象它会比这更多:

    ./configure --prefix=/home/bob
    make
    make install
    

    不需要root权限。

    【讨论】:

    • 谢谢安迪,听起来不错。我在 solaris 盒子上没有 make 可用,但我已将它传递给我的系统管理员为我构建。看看情况如何。
    • Solaris 机器上没有开发工具?这很难想象。你确定你知道它应该在哪里吗?你在 /usr/ucb 中查看过 make 和 cc 吗?
    • /usr/ucb/usr/sfw 主要在完整的 Solaris 10 安装中使用这些工具...(gmake 而不是 make 等...)
    【解决方案2】:

    comm -3 几乎可以满足您的需求,但需要排序输入。它还将按字母顺序将它们放在单独的行中。您的示例(排序后)将显示为

    five
          five5
    two
          two2
    

    如果 solaris diff 不能满足您的要求,那么标准的 solaris 机器上的任何东西都不会这样做,这意味着从其他地方引入代码,无论是您自己的还是其他人的。由于 GNU diff 可以满足您的需求,请使用它。

    【讨论】:

      【解决方案3】:

      示例输出:

      # ~/config_diff.sh postfix_DIST/master.cf postfix/master.cf
      postfix_DIST/master.cf: -o smtpd_tls_security_level=encrypt -o smtpd_sasl_auth_enable=yes -o smtpd_client_restrictions=permit_sasl_authenticated,reject -o smtpd_tls_wrappermode=yes -o smtp_fallback_relay=
      postfix/master.cf: -o cleanup_service_name=cleanup_sasl -o smtpd_tls_security_level=encrypt -o smtpd_sasl_auth_enable=yes -o smtpd_client_restrictions=permit_sasl_authenticated,reject -o cleanup_service_name=cleanup_sasl -o smtpd_tls_wrappermode=yes -o smtpd_sasl_auth_enable=yes -o smtpd_client_restrictions=permit_sasl_authenticated,reject -o smtp_fallback_relay=
      
      postfix_DIST/master.cf:smtp inet n - - - - smtpd smtp unix - - - - - smtp
      postfix/master.cf:smtp inet n - - - - smtpd smtp unix - - - - - smtp
      

      遗憾的是,它目前无法处理多个相同的配置变量......它会计算它们并认为文件不同。

      【讨论】:

      • 我让它与双配置线一起工作...... by "cksum"
      【解决方案4】:

      上面和下面给出的所有答案都是完美的,但是仅仅输入一个命令并得到一个结果并不能帮助您将来解决类似的问题。

      这里有一个链接,它解释了 diff 的工作原理。一旦你通过链接,你可以自己解决问题

      这是一个链接。 https://www.youtube.com/watch?v=5_dyVrvbWjc

      【讨论】:

        【解决方案5】:
        #! /bin/bash
        
        FILES="$@"
        
        COLUMN=1
        
        for variable in $( awk -F: '{print $X}' X=${COLUMN} ${FILES} | sort -u ) ; do
                NUM_CONFIGS=$( for file in ${FILES} ; do
                        grep "^${variable}" ${file}
                done | sort -u | wc -l )
                if [ "${NUM_CONFIGS}" -ne 1 ] ; then
                        for file in ${FILES} ; do
                                echo ${file}:$( grep "^${variable}" ${file} )
                        done
                        echo
                fi
        done
        

        【讨论】:

          猜你喜欢
          • 2011-05-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-09-16
          • 2011-05-24
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多