How to use awk
First example
This is the result of an informal computer evaluation
(SEP's Parallel Computing Tier 1991-1994).
Each column refers to a specific computer system.
Each row refers to a specific test criteria.
All numerical entries except the first column are grades.
The first column are weight factors that reflect how important
each criteria is with respect to the rest. The awk program
below prints headers for the various system, prints the table,
and computes and prints the weighted averages of the grades.
The data is to be in a file called {\tt data} and should
be in the same directory as the executable file that includes
the awk program. The program is invoked by typing its name at
the command line.
If you want to learn more about Awk, get Kernigham's book. I wish
we had such a good book for Perl.
5 1 5 3 5 Performance (Basic)
4 4 5 3 5 Performance (Tuned)
5 0 5 5 5 Programming
3 0 4 5 5 visualizing, debugging, Profiling
4 0 3 4 4 compiler
4 0 3 4 4 Avoiding computer science
3 1 3 5 5 software libraries
5 0 5 3 3 short term solution
4 5 3 3 3 long term solution
3 1 4 2 2 support
3 5 3 1 1 relationship
5 5 1 2 3 routine work, multitasking
2 0 0 5 5 past favors
3 2 4 4 4 delivery
3 1 4 2 2 knowing what we are getting
3 5 3 1 1 maintenance
2 5 3 1 1 cost
3 1 5 4 4 sponsor reaction
Here is the program:
<data awk '
BEGIN{
print "wt\tintel\tmaspar\tmc8\tmc16" # headings
FS = "\t" # input field separator
OFS = "\t" # output field separator
}
{
wt = $1 # revise the weighting function
$1 = wt # revise the weighting function
print $0 # print the line
intel += $2 * wt # accumulate weighted sums
maspar += $3 * wt
tmc8 += $4 * wt
tmc16 += $5 * wt
}
END{ # print totals.
print "\t" intel "\t" maspar "\t" tmc8 "\t" tmc16 "\t"
}'
Second example
The awk script sorts the entries in the
file. To invoke the script type enter the command:
awk -f sortAwk 1174.prb > junk
at the prompt.