Hello, I am taking an Introductory to Unix/Linux course and I have an assignment
ID: 3602297 • Letter: H
Question
Hello, I am taking an Introductory to Unix/Linux course and I have an assignment in which I have to create a script that will print the number of wines by type from a txt file. The only problem I really have is understanding how to create another file using awk or sort to separate the wine types.
The file below this drop, wine.txt, contains the records from a wine data file. The 5 fields in each row are: The wine identification number ·Wine name Type of wine (2-Sparkling, 3-Fortified, 4-Sweet, 5-White, 6-Red) Year wine was made · Winery Number This assignment requires that you create a report that will print the number of wines by type. The report should have a proper heading that includes a main heading e The main heading should include the current date The report heading should also have column headings for the type of wine and total (for the total of each type of wine) The record output should be formatted to display the data (wine type and count) below each column header The report should print on a "clean screen"Explanation / Answer
Please find the bash script below:
#! /bin/bash
date #Display date
printf "Type Of Wine Total " #Display header
#cat wines.txt | awk -F ":" '{print $3}'
sparkling=0
fortified=0
sweet=0
white=0
red=0
#Read the file line by line, awk third column and increment the counts of wine types
file="wines.txt"
while IFS= read -r line
do
let "tmp = $(echo "$line" | awk -F ":" '{print $3}')"
if [ $tmp -eq 2 ]
then
let "sparkling = sparkling + 1"
elif [ $tmp -eq 3 ]
then
let "fortified = fortified + 1"
elif [ $tmp -eq 4 ]
then
let "sweet = sweet + 1"
elif [ $tmp -eq 5 ]
then
let "white = white + 1"
elif [ $tmp -eq 6 ]
then
let "red = red + 1"
else
echo "Invalid Wine Type"
fi
done <"$file"
#Display the Count of each type of wine
printf "Sparkling %d " $sparkling
printf "Fortified %d " $fortified
printf "Sweet %d " $sweet
printf "White %d " $white
printf "Red %d " $red
OUTPUT: Using the wines.txt provoded in question, folloeing would be the output of the script
Tue May 17 21:46:56 EDT 2016
Type Of Wine Total
Sparkling 2
Fortified 2
Sweet 2
White 3
Red 1
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.