I am trying to find the oldest model from each car manufacturer in a list of unsorted cars. I am sorting this list in ascending alphabetical order, by manufacturer, model, and year with the following code:
tail +3 cars.csv > cars
shuf cars | cut -d';' -f1 > cars1
car=`head -1 cars1 | tail -1`
echo "When was the first model of the car $car made?"
read car
#Searches the oldest car model
car1=`grep "^$car" cars | sort -n -t';' -k8 | cut -d';' -f8 | head -1 | tail -1`
if [ "$car" -eq "$car1" ] ;
then
echo "Congratulations! Your answer is correct!"
else
echo "If you go through the CSV file you'll see that in this case the correct answer is $car1"
fi
rm cars 2> /dev/null
rm cars1 2> /dev/null
Imagine a case where the list goes like this:
Ford Fairmont (auto) : 78
Ford Fairmont: 80
If the question goes like this: When was the first model of the car Ford Fairmont made, and as I'm using the grep command, it will search all the lines that match with Ford Fairmont. In this case, as Ford Fairmont (auto) is the oldest one and contains the words "Ford Fairmont", the program will print the year of Ford Fairmont (auto)'s first model which is 78. How can I get rid of this problem?