Wednesday, March 12, 2008

Report Genertion with AWK


"Gyaan" International school use an UNIX based "attendance keeping software" called "regul", which generates attendance in flat file like below.
For Class: VI, the attendance of Month: June, 2007 is something like this.

$ cat attendancesum.txt
Month:June, 2007
Teacher: Mr. JKS
Class: VI

begin
Roll No:1
Days:20
end
begin
Roll No:2
Days:17
end
begin
Roll No:3
Days:16
end
begin
Roll No:4
Days:22
end
begin
Roll No:5
Days:20
end
begin
Roll No:6
Days:12
end

The purpose is build a report for students with attendance more than 19 days in that particular month.

$ awk ' BEGIN {
FS=":"
printf ("Roll\tDays\n")
printf ("------------\n")
}
END {print "------------\n"}
$1~/Roll No/ { x=$2 ; next ; }
($1~/Days/) && ($2 >= 20 ) { printf x"\t" $2"\n" ;}
' attendancesum.txt


The output would be:

Roll Days
------------
1 20
4 22
5 20
------------

No comments:

© Jadu Saikia www.UNIXCL.com