(注意,如果直接复制脚本不能用,需要去掉里面的^字符)
1,集合的交集#!/bin/bash
if [ $# == 2 ];then
u1=$1
u2=$2
elif [ $# == 3 ];then
u1=$1
u2=$2
u3=$3
else
echo "Usage:$0 set1 set2 out_set"
exit
fi
if [ $out"yes" != "yes" ];then
rm -rf $out
fi
if [ ! -f $u1 -o ! -f $u2 ];then
echo "Error:the $1 or $2 must be exist file"
exit
fi
temp1=`sort -u $u1`
temp2=`sort -u $u2`
union=`echo "$temp1 $temp2" | sort`
previous=""
for current in $union;
do
if [ $previous"yes" == "yes" ];then
previous=$current;
continue;
fi
if [ $current == $previous ];then
if [ $out"yes" != "yes" ];then
echo $current >>$out
else
echo $current
fi
previous=""
continue;
fi
previous=$current
done
if [ $# == 2 ];then
u1=$1
u2=$2
elif [ $# == 3 ];then
u1=$1
u2=$2
u3=$3
else
echo "Usage:$0 set1 set2 out_set"
exit
fi
if [ $out"yes" != "yes" ];then
rm -rf $out
fi
if [ ! -f $u1 -o ! -f $u2 ];then
echo "Error:the $1 or $2 must be exist file"
exit
fi
temp1=`sort -u $u1`
temp2=`sort -u $u2`
union=`echo "$temp1 $temp2" | sort`
previous=""
for current in $union;
do
if [ $previous"yes" == "yes" ];then
previous=$current;
continue;
fi
if [ $current == $previous ];then
if [ $out"yes" != "yes" ];then
echo $current >>$out
else
echo $current
fi
previous=""
continue;
fi
previous=$current
done
2,集合的并集
if [ $# == 2 ];then u1=$1 u2=$2 elif [ $# == 3 ];then u1=$1 u2=$2 out=$3 else echo "Usage:$0 set1 set2 out_set" exit fi if [ $out"yes" != "yes" ];then cat $u1 $u2 | sort -u | sed "/^$/d" >$out else cat $u1 $u2 | sort -u | sed "/^$/d" 3,求集合的差 #!/bin/bash if [ $# == 4 ];then u1=$1 u2=$2 out=$3 same=$4 elif [ $# == 3 ];then u1=$1 u2=$2 out=$3 elif [ $# == 2 ];then u1=$1 u2=$2 else echo "Usage:$0 set1 set2 minus_set same_set " exit fi if [ -f $out ];then rm -rf $out fi if [ -f $same ];then rm -rf $same fi declare -a main_set=(`cat $u1 |sort -u`) declare -a minus_set=(`cat $u2 |sort -u`) main_length=${#main_set[@]} minus_length=${#minus_set[@]} i=0 j=0 while [ $i -lt $main_length -a $j -lt $minus_length ] do if [ ${main_set[$i]} \< ${minus_set[$j]} ];then if [ $out"yes" != "yes" ];then echo "${main_set[$i]}">>$out else echo "${main_set[$i]}" fi i=`expr $i + 1` continue; fi if [ ${main_set[$i]} \> ${minus_set[$j]} ];then j=`expr $j + 1` continue; fi if [ ${main_set[$i]} == ${minus_set[$j]} ];then if [ $same"yes" != "yes" ];then echo "${main_set[$i]}" >>$same else echo "${main_set[$i]}" fi i=`expr $i + 1` j=`expr $j + 1` continue; fi done while [ $i -lt $main_length ] do echo "${main_set[$i]}">>$out i=`expr $i + 1` done 这些脚本里面的前两个参数都是代表文件,这个文件里面是一些记录;第三四个参数视脚本功能变化; |