a little help with using AWK to display whats being read in

I am making a script that reads in the model of a car and then searches a file and displays the make model and price of anything matching the input provided. here is what I have so far

#!/bin/sh
echo Please enter a car model:
read model

if test $? -eq 0
then
grep $model /home/cars
awk '{print $1, $2~/$model/, $3, $4}' /home/cars/
else
echo "There are no matching cars Enter a car model: "
fi

my only question is when I am using AWK, how do I display only the data for the input provided. In this case it is '$model'. For ex. say someone inputs 'Camry' I want to only use AWK to display the make model and price for models that match the word 'Camry'. In this case the models are $2. Thats where I am stuck. And the 2nd question I have is in the 2nd part where If there is no match, I re prompt, how do I get it to go back to the top of the loop to read in input and do everything over again? I want it to keep re-prompting until it matches or a user quits out. Thanks everyone any help is appreciated.

awk '$2 ~ m {print $1, $2, $3, $4}' m="${model}" /home/cars/
1 Like

this worked perfectly thanks a lot!

And do you know how I can re-prompt for input if there is no match? Basically restart the entire process.

---------- Post updated at 10:59 AM ---------- Previous update was at 10:56 AM ----------

actually I think I found it, until loop. I will read this and try my hand at it, thanks for the help

not tested....

#!/bin/sh 
while :
do
   echo 'Please enter a car model:    '
   read model     
   awk '$2 ~ m {print $1, $2, $3, $4; _e=1}END{exit _e}' m="${model}" /home/cars/    
   if [ $? -eq 0 ]; then       
      echo "There are no matching cars Enter a car model: "
   else
      exit
   fi
done
1 Like

What I did is use an until loop and it worked so far but after adding in the prompt to return an error

#!/bin/sh
echo Please enter a car model:
read model

echo $model | grep /home/cars > /dev/null
until [ $? = 0 ]
do
  echo "No matching models found."
  echo "Please enter a car model: "
  read model
  echo $model | grep /home/cars > /dev/null

done

grep $model /home/cars
awk '$2 ~ m {print $1, $2, $3, $4}' m="${model}" /home/unix/assets/cars

I'm trying to say as long as the model shows up in that file keep re prompting for a car, this is a pretty bad job on my part trying to jam stuff in there trying to get it to work. So until the argument is true that the model shows up in the /cars file, keep re prompting. That is what I am trying to do. . any advice? Thanks for your help

---------- Post updated at 12:08 PM ---------- Previous update was at 11:18 AM ----------

edit: I fixed it thank you for your help!