shell 求总分

 1 求总分并且输出:
 2 文件a.txt
 3 学号  姓名    性别     年龄
 4 1001  张三    男       18
 5 1002  赵四    男       19
 6 1003  李丽    女       18
 7 文件b.txt
 8 学号   语文   数学   英语
 9 1001   80     70     98
10 1002   78     80     70
11 1003   72     80     89
12 
13 
14 方法1:
15 #!/bin/sh
16 paste a.txt b.txt ccc.txt
17 cat ccc.txt |awk 'NR==1{ printf "%10s %10s %10s %10s %10s %10s %10s\n",$1,$2,$3,$6,$7,$8,"总分"}NR>=2{{zongfen=$6+$7+$8}{printf "%10s %10s %10s %10s  %10s %10s %10s\n",$1,$2,$3,$6,$7,$8,zongfen}}'
18 
19 方法2:
20 #!/bin/sh
21  for i in `cat aa.txt |awk '{ print $1}'`
22  do
23         a=`grep $i aa.txt |awk '{print $1}'`
24         b=`grep $i aa.txt |awk '{print $2}'`
25         c=`grep $i aa.txt |awk '{print $3}'`
26         d=`grep $i aa.txt |awk '{print $4}'`
27         e=`grep $i bb.txt |awk '{print $2}'`
28     f=`grep $i bb.txt |awk '{print $3}'`
29     g=`grep $i bb.txt |awk '{print $4}'`
30         if [ $a != "学号" ]
31         then
32                 sum=`grep $i bb.txt |awk '{$5=$2+$3+$4;print $5 }'`
33                 echo  $a"\t" $b"\t"  $c"\t" $d"\t" "$e\t"  "$f\t" $g"\t"   $sum"\t" 
34         else
35                 echo $a"\t" $b"\t"  $c"\t" $d"\t" $e"\t"  $f"\t" $g"\t"   "总分""\t"  
36         fi
37  done
38  

 

posted @ 2016-12-01 15:52  feng_kui  阅读(190)  评论(0编辑  收藏  举报