This is for my linux class and please use the awk command. Consider the data3 fi
ID: 3795624 • Letter: T
Question
This is for my linux class and please use the awk command.
Consider the data3 file below:
$ cat data3
pchen72 10
jmaszk 5
bvbui 13
mtcrowle 50
mrchave3 34
Write a awk script called activity6.1-7.awk to display the percentage of rows below and over the average.
The average is 22.4 and should be set in the BEGIN block
The output should be (result displayed with 1 decimal point):
Total number of records: 5
60.0% of the records are below the average
20.0% of the records are above the average
The header line should show: Percent above average
Explanation / Answer
$ cat ./activity6.1-7.awk
#!/bin/awk -f
BEGIN{
average = 22.4
below = 0
above = 0
total = 0
print "Percent above average"
}
{
total = total + 1
if ($2 < 22.4) { below = below +1}
else {above = above + 1}
}
END { print "Total number of records: " total; above = above*100/total; below = below*100/total; printf("%.1f", below); print "% of the records are below the average"; printf("%.1f", above); print "% of the records are above the average"}
$ cat data3
pchen72 10
jmaszk 5
bvbui 13
mtcrowle 50
mrchave3 34
$ ./activity6.1-7.awk data3
Percent above average
Total number of records: 5
60.0% of the records are below the average
40.0% of the records are above the average
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.