I want to have summary of difference between two files. Expected output is count of new, deleted and changed lines. Does diff readily provides such output? If not is there any script/utility available which helps in getting the summary.
我想要总结两个文件之间的区别。预期输出是新行,删除行和更改行的计数。差异是否容易提供这样的输出?如果没有,那么有任何脚本/实用程序可用于获取摘要。
4 个解决方案
#1
If you use diff -u it will generate a unified diff that has lines preceded with + and -. If you pipe that output through grep (to get only the + or -) and then to wc you get the counts for the + es and the - es respectively.
如果使用diff -u,它将生成一个统一的差异,其前面带有+和 - 的行。如果你通过grep管道输出(只获得+或 - )然后再输入wc,你分别获得+ es和-es的计数。
#2
I think you are looking for diffstat. Simply pipe the output of diff to diffstat and you should get something like this.
我想你正在寻找diffstat。简单地将diff的输出传递给diffstat,你应该得到这样的东西。
include/net/bluetooth/l2cap.h | 6 ++++++
net/bluetooth/l2cap.c | 18 +++++++++---------
2 files changed, 15 insertions(+), 9 deletions(-)
#3
For those using Git or Mercurial, a quick way to see such a summary of ones unstaged changes:
对于那些使用Git或Mercurial的人来说,快速查看未分阶段更改的摘要:
git diff --stat
hg diff --stat
#4
Here is the script by suyasha all formatted correctly with line breaks, with some added message output. Good job, suyasha, should have posted your reply as an answer. I would have voted for that.
这是suyasha的脚本都正确格式化了换行符,并添加了一些消息输出。干得好,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"
#1
If you use diff -u it will generate a unified diff that has lines preceded with + and -. If you pipe that output through grep (to get only the + or -) and then to wc you get the counts for the + es and the - es respectively.
如果使用diff -u,它将生成一个统一的差异,其前面带有+和 - 的行。如果你通过grep管道输出(只获得+或 - )然后再输入wc,你分别获得+ es和-es的计数。
#2
I think you are looking for diffstat. Simply pipe the output of diff to diffstat and you should get something like this.
我想你正在寻找diffstat。简单地将diff的输出传递给diffstat,你应该得到这样的东西。
include/net/bluetooth/l2cap.h | 6 ++++++
net/bluetooth/l2cap.c | 18 +++++++++---------
2 files changed, 15 insertions(+), 9 deletions(-)
#3
For those using Git or Mercurial, a quick way to see such a summary of ones unstaged changes:
对于那些使用Git或Mercurial的人来说,快速查看未分阶段更改的摘要:
git diff --stat
hg diff --stat
#4
Here is the script by suyasha all formatted correctly with line breaks, with some added message output. Good job, suyasha, should have posted your reply as an answer. I would have voted for that.
这是suyasha的脚本都正确格式化了换行符,并添加了一些消息输出。干得好,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"