BEGIN {
sum = 0;
product = 1;
sumAvg = 0;
count = 0;
}
{sum = sum + $1;}
{product = product * $2;}
{sumAvg = sumAvg + $3;
count++;
}
END {
average = sumAvg / count;
printf("Sum of column 1: ");
print sum;
printf("Product of column 2:
");
print product;
printf("Average of column 3:
");
print average;
}
This awk script prints a histogram
the size of the first data field in the file.
{
for (i = 0; i < $1;
i++) {
printf("
");
}
printf("*");
printf("\n");
}
This awk script uses the versatility
of relational arrays.
{
germTime[$1] += $2;
numPlant[$1]++;
}
END {
for(plants in germTime) {
printf("The %s's
average germination ",plants);
printf("time was
%4.1f days.\n",germTime[plants] / numPlant[plants]);
}
}
This awk script does the sum of positive
and negative numbers from a file.
{
for (i = NF; i >= 1; i--) {
printf($(i));
printf(" ");
}
printf("\n");
}
BEGIN {
sumPos = 0;
sumNeg = 0;
}
{
for (i = 1; i <= NF; i++)
{
if($(i) >= 0) {
sumPos
= sumPos + $(i);
}
else {
sumNeg
= sumNeg + $(i);
}
}
}
END {
printf("Sum of positive numbers:
");
print sumPos;
printf("Sum of negative numbers:
");
print sumNeg;
}
This is a combination of a shell-script
that calls an awk script to print the user information from the yp password
file.
#!/bin/csh -f
# Shell script to call an awk script
to print out the information
# about a user found in the /etc/passwd
file.
# Check number of arguments
if ($#argv == 0) then
echo "usage: userinfo
user-name ..."
exit 1
endif
# Run the awk script on the input
foreach f ($argv)
ypmatch $f passwd
| awk -f userinfo.awk
end
BEGIN {
FS = ":";
}
END {
printf("username:
%-30s\n",$1);
printf("password:
%-30s\n",$2);
printf("uid:
%-30s\n",$3);
printf("gid:
%-30s\n",$4);
printf("real name:
%-30s\n",$5);
printf("home directory:
%-30s\n",$6);
printf("login shell:
%-30s\n",$7);
}