专注各种脚本编程
Baidu
加入收藏夹
本站内容有下面分类知识,欢迎您的到来^_^
shell相关:指令篇 基础篇 脚本欣赏 编程实例 shell问问 shell视频教程 技巧篇 水平测试 E文资料 vi编辑器 高级Bash脚本编程指南
其他:mysql perl c语言 oracle
当前位置:| 主页>shell脚本欣赏>

一个比较两个文件的脚本

百度收藏 QQ搜藏
作者:bjgirl
来自:www.chinaunix.net
代码:
cat dfile
#!/bin/ksh
#scriptname:dfile

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>a$$
same=$(awk '$1==$2 {print $1}' a$$)
X=$(echo 两个文件相同的行是:
echo $same|tr ' ' '\n')

diff1=$(awk '$1 != $2 {print $1}' a$$|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>b$$
diff2=$(awk '$1 != $2 {print $1}' b$$|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$$
上一篇:基于图形界面的改变系统启动运行等级shell脚本 下一篇:一个定时弹出提示窗口的shell脚本

power by soyo123 2007-2008