Example of a program using 'awk'.


The following example shows how to generate a report using awk. This program reads the person.dat file (see samples) that must be stored in a unix machine.

BEGIN {

FS = "," ;
print "report_title=Employees report Example with Awk";
print "orientation=2";
print "open_view";
print "spacing=250";
print "$c0=400";
print "$c1=1000";
print "$c2=3400";
print "$c3=6800";
print "$c4=8400";
print "$c5=11000";
print "$c6=13000";
print "$c7=13200";
print "[header]";
print "{f=arial;s=8}";
print "{pic=c:\\program files\\rpv\\samples\\rpvlogo.bmp,400,400,1400,900}";
print "{\\n;$c7} Page {12800;pag}";
print "{s=16;\\n;1800;c=2;b=y} Sample Application Report {spacing=350;\\n}";
printf "{c=1;1800} Employees Report (one line per record).\n"
printf {c=0;b=n;\\n;\\n;\\n}\n";
print "{LINE=400,14000;\\n}";
printf "{s=8;b=y;$c0} ID {$c1} Name {$c2} Address {$c3} Phone {$c4} E-mail" printf "{$c5} Birth date {a=r;$c6} Mon Salary {a=l;$c7} Schedule"
printf "{b=n;spacing=375;\\n}\n";
print "{LINE=400,14000;\\n}"
print "[data]"}

{

printf "{$c0}" $1
printf "{$c1}" $2
printf "{$c2}" $3
printf "{$c3}" $4
printf "{$c4}" $6
printf "{$c5}" $7
printf "{a=r;"

{ if($8 < 1400)

{
printf "c=12;"
}

}

printf "$c6}" $8 "{c=0;a=l;$c7}" $10 "{\\n}\n"

}

This example turns in red all those monthly salaries below 1400; that is the reason of the if.
We have used print & printf commands. We took advantage of the printf command because it doesn't insert a newline.

How to run this.

1.- Just save this code and name it with anyname.awk.

2.- Place the file person.dat in the computer for the program to be able to read it.

3.- Execute this at the prompt:

# awk -f anyname.awk person.dat > listing.rpv ; mv listing.rpv /tmp/rpvqueue

 

Important aditional information