How to Prepare awk Data Report? Example

advertisements

_____________________________________________________________________________________________________________________

The Eucharistic Miracles of the World

I have a text file with following details. I wanted to create a simple report as formatted in the output. See a simple awk program to generate the report.


Content of text file
cat emp_dat
smith             800
allen            1600        300
ward             1250        500
jones            2975
martin           1250       1400
blake            2850
clark            2450
scott            3000
king             5000
turner           1500          0
adams            1100
james             950
ford             3000
miller           1300

awk program:
In the program the portions marked in red color will excute only once. Moreover the commands executed in the BEGIN and END will excute once to generate the Header and footer. The commands marked in blue will repeat for the report body.

awk 'BEGIN { print "NAME\tSALARY\t\tCOMMISSION\tTOTAL SAL";
              print "==================================================" }
      {
        printf("%s\t%8.2f\t%8.2f\t%8.2f\n", $1 ,$2 ,$3 ,$2+$3);
        total+=$2+$3;
        Totnum+=1;
      }
END {print "==================================================" ;
           printf ("Total Sal    : %d \nTotal no Emp : %d\nAverage Sal  : %d\n", total, Totnum,total/Totnum);
           print "==================================================" ;} ' emp_dat

Output
NAME    SALARY          COMMISSION      TOTAL SAL
==================================================
smith     800.00            0.00          800.00
allen    1600.00          300.00         1900.00
ward     1250.00          500.00         1750.00
jones    2975.00            0.00         2975.00
martin   1250.00         1400.00         2650.00
blake    2850.00            0.00         2850.00
clark    2450.00            0.00         2450.00
scott    3000.00            0.00         3000.00
king     5000.00            0.00         5000.00
turner   1500.00            0.00         1500.00
adams    1100.00            0.00         1100.00
james     950.00            0.00          950.00
ford     3000.00            0.00         3000.00
miller   1300.00            0.00         1300.00
==================================================
Total Sal    : 31225
Total no Emp : 14
Average Sal  : 2230
==================================================

_____________________________________________________________________________________________________________________

Website Stats

0 comments:

Post a Comment

Labels

Oracle (629) Script (86) General (77) Unix (47) Blog (23) Technology (19) gadget (6) games (6) Business (3) OCI (3) SQL* Loader (3) Datapump (2)
 

acehints.com Copyright 2011-23 All Rights Reserved | Site Map | Contact | Disclaimer