码迷,mamicode.com
首页 > 其他好文 > 详细

找出两个文本文件的不同的行

时间:2015-11-09 20:52:14      阅读:290      评论:0      收藏:0      [点我收藏+]

标签:

用shell找出两个文本文件的不同的行

亲自实验过的方法如下:
第一种:comm命令法
命令如下:comm -3 file1 file2
有一个问题就是,如果两个文件排序不一样的话,会出问题

第二种:grep命令法
命令如下:grep -vwf file1 file2
统计file1中没有,file2中有的行

具体使用环境以后再补充,今天先记录到这里。
参考文档:
1、找出两个文件内容的相同与不同:http://blog.csdn.net/shuckstark/article/details/7872176
2、comm命令:http://michaels.blogbus.com/logs/44427299.html
3、linux grep用法:http://blog.csdn.net/greytree/article/details/428532
4、linux grep命令:ttp://www.cnblogs.com/end/archive/2012/02/21/2360965.html
找出两个文件不同的数据    
#!/bin/sh 
function _diffab(){
x=0
for i in `cat $1`;do
        for j in `cat $2`;do
                if [ $i == $j ];then
                        x=1
                        break;
                fi
        done
                if [ $x -ne 1 ];then
                        echo $i
                fi
        x=0
done
}
 
if [ "$1" == "" ] || [ "$2" == "" ];then
echo "use like this: $0 filea fileb"
else
{
_diffab $1 $2
_diffab $2 $1
}
fi

 

$ comm --help
Usage: comm [OPTION]... FILE1 FILE2
Compare sorted files FILE1 and FILE2 line by line.

With no options, produce three-column output.  Column one contains
lines unique to FILE1, column two contains lines unique to FILE2,
and column three contains lines common to both files.

  -1              suppress column 1 (lines unique to FILE1)
  -2              suppress column 2 (lines unique to FILE2)
  -3              suppress column 3 (lines that appear in both files)

  --check-order     check that the input is correctly sorted, even
                      if all input lines are pairable
  --nocheck-order   do not check that the input is correctly sorted
  --output-delimiter=STR  separate columns with STR
      --help     display this help and exit
      --version  output version information and exit

Note, comparisons honor the rules specified by `LC_COLLATE.

Examples:
  comm -12 file1 file2  Print only lines present in both file1 and file2.
  comm -3  file1 file2  Print lines in file1 not in file2, and vice versa.

Report comm bugs to bug-coreutils@gnu.org
GNU coreutils home page: <http://www.gnu.org/software/coreutils/>
General help using GNU software: <http://www.gnu.org/gethelp/>
For complete documentation, run: info coreutils comm invocation

 

找出两个文本文件的不同的行

标签:

原文地址:http://www.cnblogs.com/timdes/p/4950762.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!