In: Computer Science
Create shell scripts, each in its own .sh file. Please include a shebang line at the top of the script as well as appropriate comments through out.
Write a script that reads sunspot data from data-shell/data/sunspot.txt and uses awk to print a list of the total number of sunspots for each year on record as well as the twelve monthly averages across the record.
Note: "awk allows you to index arrays using strings, so as you traverse the data file, keep running totals for the total sunspots in each year (one array indexed by year), total sunspots per month (different array indexed by month), and the total number of years (an integer). Then in the END block, print out the yearly totals and the monthly averages as two separate tables."
ANSWER :-
GIVEN THAT :-
Script.sh
#!/usr/bin/awk -f
BEGIN{
FS="[,-]"
printf
"%-10s%-10s%-10s\n","year","total","average"
print "======================================="
sum=0.0
year=""
avg=0.0
}
{
if( $1 % 12 == 0){
if(sum != 0.0){
avg = sum /
12
printf
"%-10s%-10s%-10s\n",year,sum,avg
}
sum=0.0
#print $2 "\t\t" $3 "\t" $3
}
year = $2
sum = sum + $5
}
END{
avg = sum / 12
printf "%-10s%-10s%-10s\n",year,sum,avg
print "===================Over==========="
}
SCREEN SHOT :-
THANKYOU