发现在ubuntu和centos中有些命令还不一样,比如<<<可在centos中使用,但是ubuntu中不行
csv文件名以及格式如下
3669_20180121.csv
总笔数,2,总金额,21
2018011116200603729,2018011159999248,201700198,2018/01/11 00:00,1.00,一卡通充值,120
2018011116223903730,2018011159999789,201700198,2018/01/11 00:00,2.00,一卡通充值,120
需求:
1)如果文件名相同,则把总笔数数值以及总金额数值相加,把除一行外的其他行进行追加。
2)对于文件名不通的文件,则直接放在新文件夹中。
3)新文件夹只保存最近的30个文件
方法1,可在ubuntu和centos中使用
#!/bin/bash filenum=30 dir1=./upload_root/gdby dir2=./upload_root/bzbygz dir_merge=./download #删除旧文件 rm -f $dir_merge/*.csv dir1_select=`ls $dir1 | sort -r | head -$filenum` dir2_select=`ls $dir2 | sort -r | head -$filenum` #2个文件夹共有的 echo "两个学校共有的文件名" for i in ${dir1_select} do for file2 in ${dir2_select} do if [ "$i" = "$file2" ];then echo $i num1=$(head -1 $dir1/$i | awk -F "," '{print $2}') num2=$(head -1 $dir2/$i | awk -F "," '{print $2}') money1=$(head -1 $dir1/$i | awk -F "," '{print $4}') money2=$(head -1 $dir2/$i | awk -F "," '{print $4}') num_all=$((num1+num2)) money_all=$(echo $money1 $money2 | awk '{print $1+$2}') cp $dir1/$i $dir_merge sed -ie "1 s#`echo $num1`#`echo $num_all`#; 1 s#`echo $money1`#`echo $money_all`#" $dir_merge/$i #sed '1d' $dir2/$i >> $dir_merge/$i tail -n +2 $dir2/$i >> $dir_merge/$i fi done done rm -f $dir_merge/*.csve #只在$dir1中有的: echo "只在gdby中存在的文件" for i in ${dir1_select} do for file2 in ${dir2_select} do if [ "$i" != "$file2" ];then cp $dir1/$i $dir_merge fi done done #grep -q $i <<< ${dir2_select};if [[ $? -ne 0 ]];then cp $dir1/$i $dir_merge;fi;done #只在$dir2中有的: echo "只在bzbygz存在的文件" for i in ${dir2_select} do for file1 in ${dir1_select} do if [ "$i" != "$file1" ];then cp $dir2/$i $dir_merge fi done done
方法2,只能在centos中使用
#!/bin/bash filenum=30 dir1=./bzbygz dir2=./gdby dir_merge=./download #删除旧文件 rm -f $dir_merge/*.csv dir1_select=`ls $dir1 | sort -r | head -$filenum` dir2_select=`ls $dir2 | sort -r | head -$filenum` #2个文件夹共有的 for i in ${dir1_select} do grep -q $i <<< ${dir2_select} #<<<只能在centos中使用, grep -q安静模式,不打印任何标准输出。如果有匹配的内容则立即返回状态值0。 if [[ $? -eq 0 ]];then echo $i num1=$(head -1 $dir1/$i | awk -F "," '{print $2}') num2=$(head -1 $dir2/$i | awk -F "," '{print $2}') money1=$(head -1 $dir1/$i | awk -F "," '{print $4}') money2=$(head -1 $dir2/$i | awk -F "," '{print $4}') num_all=$(($num1+$num2)) money_all=$(echo $money1 $money2 | awk '{print $1+$2}') # money_all=$(echo "$money1+$money2"|bc) cp $dir1/$i $dir_merge sed -ie "1 s#`echo $num1`#`echo $num_all`#; 1 s#`echo $money1`#`echo $money_all`#" $dir_merge/$i #sed '1d' $dir2/$i >> $dir_merge/$i #除了第一行外,进行两个文件内容的拼接, 需要在第一个文件的末尾加上一个换行符,否则会导致文件1最后一行和文件2第一行在一行 echo -e " " >> $dir_merge/$i tail -n +2 $dir2/$i >> $dir_merge/$i sed -i '/^$/d' $dir_merge/$i #删除所有空白行 sed -i 's/ //g' $dir_merge/$i #去掉^M fi done rm -f $dir_merge/*.csve #只在$dir1中有的: for i in ${dir1_select};do grep -q $i <<< ${dir2_select};if [[ $? -ne 0 ]];then cp $dir1/$i $dir_merge;fi;done #只在$dir2中有的: for i in ${dir2_select};do grep -q $i <<< ${dir1_select};if [[ $? -ne 0 ]];then cp $dir2/$i $dir_merge;fi;done
三. 执行定时任务时不执行
解决方法,crontab -e中这样写,并添加脚本的执行权限。
SHELL=/bin/bash
0 3 * * * cd /data/sftp/mysftp/ && ./file_merge.sh