标签:shell awk
需求:对文件中第一列相同的关键字的行进行sum,文件内容如下文件内容:
Insert 170
Total_Data_Bytes 3886
Delete 0
Insert 170
Total_Data_Bytes 3886
Delete 0
Insert 0
Insert 170
Total_Data_Bytes 3886
Delete 0
Insert 0
FieldComp 0
Insert 170
Total_Data_Bytes 3886
Delete 0
Insert 0
FieldComp 0
GGSPKUpdate 0
Insert 170
Total_Data_Bytes 3886
Delete 0
Insert 0
FieldComp 0
GGSPKUpdate 0
GGSPurgedata 0
Insert 170
Total_Data_Bytes 3886
Delete 0
Insert 0
FieldComp 0
GGSPKUpdate 0
GGSPurgedata 0
Total_Data_Bytes 0
处理后效果(分别统计Delete,FieldComp,GGSPKUpdate,GGSPurgedata,Insert,Total_Data_Bytes相同的行进行求和)
Delete: 0
FieldComp: 0
GGSPKUpdate: 0
GGSPurgedata: 0
Insert: 1020
Total_Data_Bytes: 23316
shell代码:
var_text=`cat /u01/app/oracle/ogg/ogg_lastdata/49/GRMCDB.CBS_T2.txt|awk '{print $1}'|sort|uniq > /home/oracle/oggscript/vartextlist`
while read line
do
var1=$line
case $var1 in
"Delete")
grep "Delete" /u01/app/oracle/ogg/ogg_lastdata/49/GRMCDB.CBS_T2.txt|awk 'BEGIN {count=0} {count+=$2;} END {print "Delete:",count}';;
"FieldComp")
grep "FieldComp" /u01/app/oracle/ogg/ogg_lastdata/49/GRMCDB.CBS_T2.txt|awk 'BEGIN {count=0} {count+=$2;} END {print "FieldComp:",count}';;
"GGSPKUpdate")
grep "GGSPKUpdate" /u01/app/oracle/ogg/ogg_lastdata/49/GRMCDB.CBS_T2.txt|awk 'BEGIN {count=0} {count+=$2;} END {print "GGSPKUpdate:",count}';;
"GGSPurgedata")
grep "GGSPurgedata" /u01/app/oracle/ogg/ogg_lastdata/49/GRMCDB.CBS_T2.txt|awk 'BEGIN {count=0} {count+=$2;} END {print "GGSPurgedata:",count}';;
"Insert")
grep "Insert" /u01/app/oracle/ogg/ogg_lastdata/49/GRMCDB.CBS_T2.txt|awk 'BEGIN {count=0} {count+=$2;} END {print "Insert:",count}' ;;
"Total_Data_Bytes")
grep "Total_Data_Bytes" /u01/app/oracle/ogg/ogg_lastdata/49/GRMCDB.CBS_T2.txt|awk 'BEGIN {count=0} {count+=$2;} END {print "Total_Data_Bytes:",count}';;
esac
done < /home/oracle/oggscript/vartextlist
标签:shell awk
原文地址:http://blog.51cto.com/11310506/2106436