Hash(){
if [ "$#" -ne "1" ]; then
echo "Hash Wrong Paramenters!" >&2
return 1
fi
read string
length=`echo $string|awk '{printf("%d", length($0))}'`
if [ "$length" -eq "0" ]; then
echo "Hash: At least one letter should be input!" >&2
return 1
fi
iCount=1
letterSum=0
while [ "$iCount" -le "$length" ]
do
letterCur=`expr substr $string $iCount 1`
letterNumCur=`printf "%d" "'$letterCur"`
#echo "$letterCur $letterNumCur"
letterSum=`expr $letterSum + $letterNumCur`
iCount=`expr $iCount + 1`
done
#echo $letterSum
letterSum=`expr $letterSum % $1`
echo $letterSum
return 0
}
#根据文件的第一个域的hash值分割文件
#SplitByHashfun Inputfile OutputFolder nParts
#带3个参数,分别表示待分割的文件,输出目录,最大分成的份数(有可能不足这么多份)
SplitByHashfun(){
if [ "$#" -ne "3" ]; then
echo "SplitByHashfun: Wrong Paramenters!" >&2
echo "Usage: SplitByHashfun Inputfile OutputFolder nParts"
return 1
fi
if [ ! -d $2 ]; then
echo "SplitByHashfun: OutputFolder $2 is not existed!" >&2
return 1
fi
echo "Spliting file $1 ......"
filename=`basename $1`
while read aLine
do
hashnum=`echo $aLine|awk '{print $1}'|Hash $3`
#echo $hashnum
fileid=`echo $hashnum|awk '{printf("%02d", $1)}'`
outfile=$2/${filename}_${fileid}
if [ -f $outfile ]; then
echo $aLine >>$outfile
else
echo $aLine >$outfile
fi
done <$1
}
#read letter
#echo $letter|Hash 10
SplitByHashfun testhash.txt ./result 5
以文件的某一域分割文件
最新推荐文章于 2023-10-25 15:01:29 发布