Linux command line Question (AWK): pizzaOrders.txt appears as below: ID TP NP NS
ID: 3748006 • Letter: L
Question
Linux command line Question (AWK):
pizzaOrders.txt appears as below:
ID TP NP NS NC TC
ID_1 5 3 2 0 28
ID_2 1 0 0 1 5.0
ID_3 7 2 2 3 37.5
ID_4 11 3 3 5 58.75
Step 7: Use the awk command pizzaOrders.txt Column Descriptions ID Order IDentification Number TP - Total Number of Pizzas Ordered NP Number of Pepperoni Pizzas Ordered ($5.50) NS Number of Sausage Pizzas Ordered ($5.75) NC Number of Cheese Pizzas Ordered ($5.00) TC Total Cost A. Using pizzaOrders.txt, print out the average cost per pizza for each order. B. Using pizzaOrders.txt, calculate and print the percent of all pizzas sold that were cheeseExplanation / Answer
Hi,
Below is the complete code-
# Hello World Program in Bash Shell
echo "ID TP NP NS NC TC" > pizzaOrders.txt
echo "ID_1 5 3 2 0 28" >> pizzaOrders.txt
echo "ID_2 1 0 0 1 5.0" >> pizzaOrders.txt
echo "ID_3 7 2 2 3 37.5" >> pizzaOrders.txt
echo "ID_4 11 3 3 5 58.75" >> pizzaOrders.txt
cat pizzaOrders.txt
#Answer A
cat pizzaOrders.txt | awk 'NR == 1 { print $1, "Average"; next } # Print a heading row
NF > 2 {
sum=0;
cost=0;
for (i=3; i<=NF-1; i++)
sum+=$i;
cost=$3*5.50+$4*5.75+$5*5.00
print $1, (cost/sum)}'
#Answer B
cat pizzaOrders.txt | awk '{
split($0,a," ");
totalsum += a[2]
cheesesum+=a[5]
}
END {print "The cheese percent is " cheesesum/totalsum*100}'
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.