-1

I have a text file as below:

sales|100
accounts|500
marketing|200 
sales|50
sales|70 
accounts|300
marketing|40
accounts|150
marketing|200 
sales|80

with the following script, I have generated output:

sort -t"|" -k1 a.txt|awk -F"|" -v "mth=$month" 'BEGIN { ln=0;slno=0;pg=0;dept=0 }
{
dept=$1;
{
if (dept !=x||ln > 50)
{ 
tot=0;
pg++;
printf ("\f");
#printf ("\n");
print "DEPT SUMMARY.FOR",dept,"DEPT. Page No-",pg
print "+=============================================+"
print "|Sr.|Dept      |Amount Rs.                    |"
print "+=============================================+"
ln=0;pg=0;
}
if (dept!=x)
slno=1;
{
printf ("|%3s|%10s|%10.2f|\n",slno,$1,$2);
ln++;
slno++;
x=dept;
 }
}
} END {
printf ("\f"); }' > summary

which gives output like this.

    DEPT SUMMARY.FOR accounts DEPT. Page No- 1
+=============================================+
|Sr.|Dept      |Amount Rs.                    |
+=============================================+
|  1|  accounts|    150.00|
|  2|  accounts|    300.00|
|  3|  accounts|    500.00|
    DEPT SUMMARY.FOR marketing DEPT. Page No- 1
+=============================================+
|Sr.|Dept      |Amount Rs.                    |
+=============================================+
|  1| marketing|    200.00|
|  2| marketing|    200.00|
|  3| marketing|     40.00|
    DEPT SUMMARY.FOR sales DEPT. Page No- 1
+=============================================+
|Sr.|Dept      |Amount Rs.                    |
+=============================================+
|  1|     sales|    100.00|
|  2|     sales|     50.00|
|  3|     sales|     70.00|
|  4|     sales|     80.00|

I want output like this but I am not able to do this; I've tried lot of modifications.

    DEPT SUMMARY.FOR accounts DEPT. Page No- 1
+=============================================+
|Sr.|Dept      |Amount Rs.                    |
+=============================================+
|  1|  accounts|    150.00|
|  2|  accounts|    300.00|
|  3|  accounts|    500.00|
-----------------------------------------------
Total               950.00
-----------------------------------------------
    DEPT SUMMARY.FOR marketing DEPT. Page No- 1
+=============================================+
|Sr.|Dept      |Amount Rs.                    |
+=============================================+
|  1| marketing|    200.00|
|  2| marketing|    200.00|
|  3| marketing|     40.00|
-----------------------------------------------
Total               440.00
-----------------------------------------------
    DEPT SUMMARY.FOR sales DEPT. Page No- 1
+=============================================+
|Sr.|Dept      |Amount Rs.                    |
+=============================================+
|  1|     sales|    100.00|
|  2|     sales|     50.00|
|  3|     sales|     70.00|
|  4|     sales|     80.00|
-----------------------------------------------
Total               300.00
-----------------------------------------------

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.