Answer:
Check the explanation
Step-by-step explanation:
Script:
#!/usr/bin/ksh
#using awk to manipulated the input file
if [ $# != 1 ]
then
echo "Usage: 08-numMajors.sh <file-name>"
exit 1
fi
if [ -f $1 ]
then
awk '
BEGIN {
FS=","
i=0
flag=0
major[0]=""
output[0]=0
total=0
}
{
if ($3)
{
for (j=0; j<i; j++)
{
# printf("1-%s-%s\\", major[j], $3);
if(major[j] == $3)
{
flag=1
output[j] = output[j] + 1;
total++
}
}
if (flag == 0)
{
major[i]=$3
output[i] = output[i] + 1;
i++;
total++
}
else
{
flag=0
}
}
}
END {
for (j=0; j<i; j++)
{
if(output[j] > 1)
printf("There are %d `%s` majors\\", output[j], major[j]);
else
printf("There is %d `%s` major\\", output[j], major[j]);
}
printf("There are a total of %d students in this class\\", total);
}
' < $1
else
echo "Error: file $1 does not exist"
fi
Output:
USER 1> sh 08-numMajors.sh sample.txt
There are 4 ` Computer Information Systems` majors
There is 1 ` Marketing` major
There are 2 ` Computer Science` majors
There are a total of 7 students in this class
USER 1> sh 08-numMajors.sh sample.txtdf
Error: file sample.txtdf does not exist
USER 1> sh 08-numMajors.sh
Usage: 08-numMajors.sh <file-name>
USER 1> sh 08-numMajors.sh file fiel2
Usage: 08-numMajors.sh <file-name>