0. 综述: 目前据我所了解的有如下几种方法,来比较两个文件的行,以输出他们的不同之处: 1) comm mission: comm [-123] file1 file2 [> sfile] 2) diff mission: diff [-opt] file1 file2 3) grep mission: grep -v -f file1 file2 /*注::此法在对比数字时候比较凑效果,文本对比不建议使用*/ 4) awk mission: awk '{print NR,$0}' file1 file2 |sort -k2|uniq -u -f 1|sort -k1|awk '{print $2}' 或者: awk '{print $0}' file1 file2 |sort|uniq -u 5) join mission: join [-i][-a<1或2>][-e<字符串>][-o<格式>][-t<字符>][-v<1或2>][-1<栏位>][-2<栏位>] comm [-123] file1 file2 [> sfile] 该命令是对两个已经排好序的文件进行比较。其中file1和file2是已排序的文件。comm读取这两个文件,然后生成三列输出:仅在file1中出现 的行;仅在file2中出现的行;在两个文件中都存在的行。如果文件名用“- ”,则表示从标准输入读取。
选项1、2或3抑制相应的列显示。例如comm - 12就只显示在两个文件中都存在的行;comm - 23只显示在第一个文件中出现而未在第二个文件中出现的行;comm - 123则什么也不显示。 2. diff 命令详解 (http://www./detail976.shtml) (3)选项: -b忽略空格造成的差别。例如,"How are you”与“How are you”被看做是相同的字符串。 colordiff file1 file2 你也可以将 ColorDiff 用于 diff 命令的管道输出:diff -u file1 file2 | colordiff 3. grep 命令详解http://www./200902/linux-grep.html http://blog./linux/1008.html http://blog.csdn.net/tenfyguo/article/details/6387786 http://www./xueyuanzhuanqu/jishuwenzhang/201111/jishuwenzhang-1168.html 4. awk 命令详解 http://www./index-Index-show-view22152.html 5. join 命令详解 功能说明:将两个文件中,指定栏位内容相同的行连接起来。 语 法:join [-i] [-a<1或2>][-e<字符串>][-o<格式>][-t<字符>][-v<1或 2>][-1<栏位>][-2<栏位>][--help][--version][文件1][文件2] 补充说明:找出两个文件中,指定栏位内容相同的行,并加以合并,再输出到标准输出设备。 参 数: -a<1或2> 除了显示原来的输出内容之外,还显示指令文件中没有相同栏位的行。 -e<字符串> 若[文件1]与[文件2]中找不到指定的栏位,则在输出中填入选项中的字符串。 -i或--igore-case 比较栏位内容时,忽略大小写的差异。 -o<格式> 按照指定的格式来显示结果。 -t<字符> 使用栏位的分隔字符。 -v<1或2> 跟-a相同,但是只显示文件中没有相同栏位的行。 -1<栏位> 连接[文件1]指定的栏位。 -2<栏位> 连接[文件2]指定的栏位。 --help 显示帮助。 --version 显示版本信息。 links: http:///unixcd12/article_5023.html http://www./2009/11/17/1642/ http://codingstandards./blog/796299 自己写的一个例子 #!/bin/sh #2012-06-14 #List format is #C4 189751 27251 1 #C1 189751 66396 1 #C1 189751 89702 1 time=`date +%y%m%d%H%M%S` echo "start at 20"$time fHead=ee fTail=.txt file1=$fHead$fTail file2="sf-"$fHead$fTail fDiff=$fHead"Diff"$fTail rm $fDiff sed -i 's/ /_/g' $file1 sed -i 's/ /_/g' $file2 time=`date +%y%m%d%H%M%S` echo "step 1: done the sed s/ /_/g on 20"$time awk '{print $0}' $file1 $file2 |sort|uniq -u > $fDiff time=`date +%y%m%d%H%M%S` echo "step 2: done the awk diff on 20"$time fTemp=$fHead"TempDiff"$fTail fNew=$fHead"DetailDiff"$fTail rm $fTemp $fNew cp $fDiff $fTemp for i in `cat $fTemp` do line="" line=`cat $file1 | grep $i` #echo "1: line is " $line if [ "$line" != "" ] then echo "only mine have:" $line >> $fNew sed -i '/'$i'/d' $fTemp fi line="" line=`cat $file2 | grep $i` #echo "2: line is " $line if [ "$line" != "" ] then echo "only Bham have:" $line >> $fNew sed -i '/'$i'/d' $fTemp fi done time=`date +%y%m%d%H%M%S` echo "step 3: Done on 20"$time |
|