代码之家  ›  专栏  ›  技术社区  ›  Michiel Overeem

如何让diff报告新行、更改行和删除行的摘要

  •  41
  • Michiel Overeem  · 技术社区  · 6 年前

    我想总结一下两个文件之间的区别。预期的输出是新行、已删除行和已更改行的计数。diff是否容易提供这种输出?如果没有,是否有任何脚本/实用程序可用于获取摘要。

    4 回复  |  直到 6 年前
        1
  •  13
  •   lothar    15 年前

    如果使用diff-u,它将生成一个前面有行的统一diff + - . 如果将输出通过grep(仅获取 + - )然后去厕所,你会得到 + ES和 - 分别是。

        2
  •  87
  •   Rob    6 年前

    我想你在找 diffstat . 简单的管道输出 diff 弥散器 你应该得到这样的东西。

     include/net/bluetooth/l2cap.h |    6 ++++++
     net/bluetooth/l2cap.c         |   18 +++++++++---------
     2 files changed, 15 insertions(+), 9 deletions(-)
    
        3
  •  16
  •   stefano    8 年前

    对于那些使用 吉特 水银的 很快就可以看到这样的总结 未老化的变化 :

    git diff --stat
    hg diff --stat
    
        4
  •  2
  •   Bill-G    8 年前

    这是Suyasha编写的脚本,格式正确,带有换行符,并添加了一些消息输出。干得好,苏亚莎,应该把你的回答作为答案。我会投赞成票的。

    #!/bin/bash
    # USAGE:    diffstat.sh [file1] [file2]
    
    if [ ! $2 ]
    then
       printf "\n   USAGE: diffstat.sh [file1] [file2]\n\n"
       exit
    fi
    
    diff -u -s "$1" "$2" > "/tmp/diff_tmp" 
    add_lines=`cat "/tmp/diff_tmp" | grep ^+ | wc -l`
    del_lines=`cat "/tmp/diff_tmp" | grep ^- | wc -l` 
    # igonre diff header (those starting with @@) 
    at_lines=`cat "/tmp/diff_tmp" | grep ^@ | wc -l`
    chg_lines=`cat "/tmp/diff_tmp" | wc -l`
    chg_lines=`expr $chg_lines - $add_lines - $del_lines - $at_lines` 
    # subtract header lines from count (those starting with +++ & ---) 
    add_lines=`expr $add_lines - 1`
    del_lines=`expr $del_lines - 1`
    total_change=`expr $chg_lines + $add_lines + $del_lines`
    rm /tmp/diff_tmp
    
    printf "Total added lines:  "
    printf "%10s\n" "$add_lines"
    printf "Total deleted lines:"
    printf "%10s\n" "$del_lines"
    printf "Modified lines:     "
    printf "%10s\n" "$chg_lines"
    printf "Total changes:      "
    printf "%10s\n" "$total_change"