shell(哈希表应用)

假设场景:

       一个文件(或是一个表)里面有各个超市商品的成本价,另一个文件(或是一个表)里面是商品的售价,求取商品利润。

字段1(商品)   字段2(超市类型) 字段3(成本价)

面包   1  8
方便面 2  7
可乐   3  4
雪糕   1  2

字段1(商品)   字段2(超市类型) 字段3(售价)

面包   1  12
方便面 2  10
可乐   3  6
雪糕   1  5

结果:

唐久方便面	7 10 3 
美特好可乐	4 6  2 
金虎雪糕	        2 5  3 
金虎面包	        8 12 4 

shell代码:

intxt=$1
outtxt=$2
cat ${intxt} |awk -v out=${outtxt} 'BEGIN{
        M[1]="金虎";
        M[2]="唐久";
        M[3]="美特好";
        while ( getline value < out ) {
            num=split(value,tmp," ")
            if ( num>0 ) {
               key=sprintf("%s|%s",M[tmp[2]],tmp[1]);
               H[key]=tmp[3];
            }
        }
}{
        newkey=sprintf("%s|%s",M[$2],$1);
	portin=$3;
        portout=H[newkey];
        #inNum

猜你喜欢

转载自blog.csdn.net/m0_37806112/article/details/104027595