免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 819 | 回复: 0
打印 上一主题 下一主题

比较文件的不同 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-08-28 10:26 |只看该作者 |倒序浏览
文件a.txt、b.txt内容如下
#more a.txt
11
22
33
#more b.txt
11
33
要求取出a.txt中存在, b.txt中不存在的内容,这里为 22
方法1 --- grep
#grep -v -f b.txt a.txt
22
-f FILE, --file=FILE
              Obtain patterns from FILE, one per line.  The empty
              file contains zero patterns, and therefore  matches
              nothing.
-v, --invert-match
              Invert the sense of matching, to select  non-matching lines.
方法2 -- 脚本(未验证)
#!/bin/ksh
#
#
function Usage {
echo "Usage: dfile [-x|-y|-z] file1 file2"
}
(($# != 3 )) && { Usage; exit 1; }
for i in $(cat $2);do
        for j in $(cat $3);do
        echo $i $j
        done
done|tee a$$ b$$>/dev/null
same=$(awk '$1==$2 {print $1}' a$$)
X=$(echo "两个文件相同的行是:"echo $same|tr ' ' '\n')
diff1=$(awk '$1 != $2 {print $1}' b$$|uniq -d )
Y=$(echo "文件$2中存在而文件$3中不存在的行是:"
echo $same $diff1|tr ' ' '\n'|sort|uniq -u
echo "\n")
for n in $(cat $3);do
        for m in $(cat $2);do
        echo $n $m
        done
done|tee x$$ y$$>/dev/null
diff2=$(awk '$1 != $2 {print $1}' y$$|uniq -d )
Z=$(echo "在文件$3中存在而文件$2中不存在的行是:"
echo -n $diff2 $same|tr ' ' '\n'|sort|uniq -u
echo "\n")
while getopts xyz arg
do
        case $arg in
        x)      echo $X|tr ' ' '\n';;
        y)      echo $Y|tr ' ' '\n';;
        z)      echo $Z|tr ' ' '\n';;
        \?)     echo "选项错误"; Usage;;
        esac
done
rm a$$ b$$ x$$ y$$  


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/70323/showart_1150749.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP