文本比对shell

前端之家收集整理的这篇文章主要介绍了文本比对shell前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1.提取源端和目标端单独存在的表名在前两个区域显示提示alone
2.判断剩下共有的表的数据差距,若超出阈值,则在第三个区域进行显示提示abnormal
3.判断剩下共有的表的数据差距,若相等或在阈值之内,则认为正常进行输出提示normal
用法 sh showdiff A.txt B.txt 10(阈值)
A.txt
名称 数值

B.txt
名称 数值

输出结果:

table in this section are alone in src:have no one

table in this section are alone in src:have no one

tables in this section are abnormal:

tables in this section are normal:

#!/bin/bash 
sort $1 -k 1 > src_sorted.tmp
sort $2 -k 1 > dst_sorted.tmp



echo -e "\n\n" > compare_results.txt;
awk '{printf "%s\n",$1}' src_sorted.tmp  | sort -k 1 > srcname_sorted.tmp
awk '{printf "%s\n",$1}' dst_sorted.tmp  | sort -k 1 > dstname_sorted.tmp

#find alone table_names and prevent them into the diff caculation
comm srcname_sorted.tmp dstname_sorted.tmp -2 -3 > src_alone.tmp
comm srcname_sorted.tmp dstname_sorted.tmp -1 -3 > dst_alone.tmp

grep "." src_alone.tmp > /dev/null


#find alone tables in src and show them
if [ $? -eq 0 ];then
 tables_cnt=`cat src_alone.tmp | wc -l`
 echo -e "$tables_cnt tables in this section are alone in src:\n" >> compare_results.txt;
 awk '{printf "\" %s \"\n",$1}' src_alone.tmp |  xargs -I {} grep {} src_sorted.tmp  | xargs printf "%50s\t%20d\n"  >>  compare_results.txt;

else

 echo -e "table in this section are alone in src:have no one\n" >> compare_results.txt;

fi


#find alone tables in dst and show them
grep "." dst_alone.tmp > /dev/null
if [ $? -eq 0 ];then

 tables_cnt=`cat dst_alone.tmp | wc -l`
 echo -e "$tables_cnt tables in this section are alone in dst:\n" >> compare_results.txt; 
 awk '{printf "\" %s \"\n",$1}' dst_alone.tmp | xargs -I {} grep {} dst_sorted.tmp  | xargs printf "%50s\t%20d\n"  >>  compare_results.txt;

else

 echo -e "table in this section are alone in src:have no one\n" >> compare_results.txt;

fi


#produce sed args to elimate alone table_name in both sorted text
grep "." src_alone.tmp > /dev/null

#elimate alone table in src
if [ $? -eq 0 ];then

 awk '{printf "\"/ %s .*/d\"\n",$1}' src_alone.tmp | xargs -I {} sed -i {} src_sorted.tmp
 sort -k 1 src_sorted.tmp  > src_elimated_sorted.tmp

else

 cp src_sorted.tmp src_elimated_sorted.tmp

fi



grep "." dst_alone.tmp > /dev/null

#elimate alone table in dst
if [ $? -eq 0 ];then

 awk '{printf "\"/ %s .*/d\"\n",$1}' dst_alone.tmp | xargs -I {} sed -i {} dst_sorted.tmp 
 sort -k 1 dst_sorted.tmp > dst_elimated_sorted.tmp

else

 cp dst_sorted.tmp dst_elimated_sorted.tmp

fi



#find data of which diff is in the threshold
comm src_elimated_sorted.tmp dst_elimated_sorted.tmp -2 -3 > src_elimated_sorted_diff.tmp
comm src_elimated_sorted.tmp dst_elimated_sorted.tmp -1 -3 > dst_elimated_sorted_diff.tmp



paste src_elimated_sorted_diff.tmp dst_elimated_sorted_diff.tmp | awk -v thres=$3 'BEGIN{ print "tables in this section are abnormal:" >> "compare_results.txt"; printf "%50s\t%20s\t%20s\t%20s\n","TABLE NAME","SROUUCE COUNT","DEST COUNT","DIFFERENCE" >> "compare_results.txt"; } { table_name=$1; count_src=$2; count_dst=$4 diff=count_src-count_dst; if ((diff+0 <= thres) && (diff+0 >= -thres)) { #this data of which diff is in threshold,but not equal printf "%50s\t%20d\t%20d\t%20d\n",table_name,count_src,count_dst,diff > "inthreshold.tmp"; } else { #data of which diff is not in the threshold printf "%50s\t%20d\t%20d\t%20d\n",diff > "compare_results.txt"; } }' 


echo "--------------------------------------------------------------------------------------------------------------------------------------" >> compare_results.txt
#sort data diff is in threshold or is equal
comm src_elimated_sorted.tmp dst_elimated_sorted.tmp -1 -2 | awk '{printf "%50s\t%20d\t%20d\t%20d\n",$1,$2,0}'>> inthreshold.tmp 
cat inthreshold.tmp | sort -k 1 > inthreshold_sorted.tmp


echo "tables in this section are normal:" >> compare_results.txt;

#format the result(data in theshold)
printf "%50s\t%20d\t%20d\t%20d\n" $(cat inthreshold_sorted.tmp) >> compare_results.txt


#rm -rf *.tmp
原文链接:https://www.f2er.com/bash/389581.html

猜你在找的Bash相关文章