线上有这样的一个需求,有两个集合, A   B  求 A-B  是什么,用shell 脚本 实现

#!/bin/bash
curDir=$(cd "$(dirname "${BASH_SOURCE[0]}")"; pwd);    #当前绝对路径
newfile=$curDir/difference_set
# get_diff_set.sh 
#  求差集 ,A -B 的集合  , 把结果 接到newfile 文件中
# $1   是指 A 集合
# $2   是指 B集合 
# 该脚本 功能  A -B 的差集结果   所求的 结果放在  当前目录下面 difference_set

cat /dev/null > ${newfile}
sumlinenumber=`wc -l $1|awk '{print $1}'`
#echo "sumline:$sumlinenumber"

for  line  in ` seq  1  $sumlinenumber `;do
        regular_tag=`sed -n "$line"p $1`
#       echo "this $regular_tag:$regular_tag"
        if   sed -n "$line"p $1| grep --color  -q  -w -E "${regular_tag}"  $2 ;then
            echo "this $line line is equal.." > /dev/null
        else
             sed -n "$line"p  $1 >> $newfile
        fi

#    echo "curline: $line"
done

[root@bdc128 4444]# cat a.txt 
Hello 
from
the
other
side 
I 
must
have
called 
a 
thousand
times 
[root@bdc128 4444]# cat b.txt 
the
other
side 
I 
must
a 
thousand
times 
bbbb


可以看出 结果没有问题,  这样就可以求 A-B    即  A与B的差集 .




                分享快乐,留住感动                                                                               ---标哥      20170831-2311