Unix Shell - Summary Report using awk

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > October 2006 > Summary Report using awk





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Summary Report using awk
Rasheed

2006-10-20, 1:27 am

Can any body tell me how to print summary report of employee for the
whole year.
Following is the file which contains the number of hours each employee
has worked.

Emloyee Month Designation HoursWorked
A Jan clerk (02:45)
B Jan Salesman (02:12)
C Jan Accountant (03:12)
A Feb clerk (01:10)
B Feb Salesman (11:10)
B March Salesman (3:10)
C Feb Accountant (3:34)

Using awk I want to print report for each employe showing the total
number of hours and minutes he has worked
e.g
Employee Total hours
A Total hours of A
B Total hours of B
C Total hours of C

John W. Krahn

2006-10-20, 7:21 am

Rasheed wrote:
> Can any body tell me how to print summary report of employee for the
> whole year.
> Following is the file which contains the number of hours each employee
> has worked.
>
> Emloyee Month Designation HoursWorked
> A Jan clerk (02:45)
> B Jan Salesman (02:12)
> C Jan Accountant (03:12)
> A Feb clerk (01:10)
> B Feb Salesman (11:10)
> B March Salesman (3:10)
> C Feb Accountant (3:34)
>
> Using awk I want to print report for each employe showing the total
> number of hours and minutes he has worked
> e.g
> Employee Total hours
> A Total hours of A
> B Total hours of B
> C Total hours of C


$ echo "
Emloyee Month Designation HoursWorked
A Jan clerk (02:45)
B Jan Salesman (02:12)
C Jan Accountant (03:12)
A Feb clerk (01:10)
B Feb Salesman (11:10)
B March Salesman (3:10)
C Feb Accountant (3:34)
" | PERL -ane'
( $h, $m ) = $F[ -1 ] =~ /\d+/g or next;
$t{ $F[ 0 ] } += $h * 60 + $m;
}{
printf "%-12s %s\n", "Employee", "Total hours";
printf "%-12s %02d:%02d\n", $_, $t{$_} / 60, $t{$_} % 60 for sort keys %t
'
Employee Total hours
A 03:55
B 16:32
C 06:46


John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
Ed Morton

2006-10-20, 1:22 pm

Rasheed wrote:

> Can any body tell me how to print summary report of employee for the
> whole year.
> Following is the file which contains the number of hours each employee
> has worked.
>
> Emloyee Month Designation HoursWorked
> A Jan clerk (02:45)
> B Jan Salesman (02:12)
> C Jan Accountant (03:12)
> A Feb clerk (01:10)
> B Feb Salesman (11:10)
> B March Salesman (3:10)
> C Feb Accountant (3:34)
>
> Using awk I want to print report for each employe showing the total
> number of hours and minutes he has worked
> e.g
> Employee Total hours
> A Total hours of A
> B Total hours of B
> C Total hours of C
>


awk 'NR==1 { next }
{ gsub(/[()]/,""); split($NF,t,":"); hours[$1] += (t[1] + (t[2]/60)) }
END{ printf "%s%15s\n", "Employee", "Total hours"
for (e in hours) printf "%s%20.2f\n", e, hours[e]}' file1

Regards,

Ed.
Kenny McCormack

2006-10-20, 1:22 pm

In article <Qd0_g.27957$H7.13719@edtnps82>,
John W. Krahn <krahnj@telus.net> wrote:
....
>$ echo "
>Emloyee Month Designation HoursWorked
>A Jan clerk (02:45)
>B Jan Salesman (02:12)
>C Jan Accountant (03:12)
>A Feb clerk (01:10)
>B Feb Salesman (11:10)
>B March Salesman (3:10)
>C Feb Accountant (3:34)
>" | PERL -ane'


What part of "... using awk" are you unable to understand?

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com