Code:
#!/bin/sh
JOBFOLDER=/opt/hpx/exstream/StatementAppA/inputs/POC/testSort
input_file=$JOBFOLDER/Input.dat
count_output_file=$JOBFOLDER/Count_output.dat
final_updated_file=$JOBFOLDER/Final_updated.dat
awk ' BEGIN {FS=" "}
function round(A) {
return int( A + 0.5 )
}
{a[$3]+=$7; b[$3]+=$9} END{for(x in a) print x,round((a[x]+b[x])/2)
}' $input_file >>$count_output_file
echo "--- `basename $count_output_file `---"
cat $count_output_file
echo "---------------- ` basename $final_updated_file ` --------------"
awk ' BEGIN {FS=" "} FNR==NR{a[$1]=$2;next}
{
if(a[$3] >= 0 && a[$3] <= 9)
{
print $1" ",$2,$3,$4" ",$5" ",$6,$7,a[$3]?"00000"a[$3]:"NA",$9
}
else if(a[$3] >= 10 && a[$3] <= 99)
{
print $1" ",$2,$3,$4" ",$5" ",$6,$7,a[$3]?"0000"a[$3]:"NA",$9
}
else if(a[$3] >= 100 && a[$3] <= 999)
{
print $1" ",$2,$3,$4" ",$5" ",$6,$7,a[$3]?"000"a[$3]:"NA",$9
}
else if(a[$3] >= 1000 && a[$3] <= 9999)
{
print $1" ",$2,$3,$4" ",$5" ",$6,$7,a[$3]?"00"a[$3]:"NA",$9
}
else if(a[$3] >= 10000 && a[$3] <= 99999)
{
print $1" ",$2,$3,$4" ",$5" ",$6,$7,a[$3]?"0"a[$3]:"NA",$9
}
else if(a[$3] >= 100000 && a[$3] <= 999999)
{
print $1" ",$2,$3,$4" ",$5" ",$6,$7,a[$3]?a[$3]:"NA",$9
}
}' $count_output_file $input_file >> $final_updated_file
echo "----------------------------------------------------"
cat $final_updated_file
rm $final_updated_file $count_output_file