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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting a little help with using AWK to display whats being read in
# 1  
Old 04-25-2012
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

Code:
#!/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.
# 2  
Old 04-25-2012
Code:
awk '$2 ~ m {print $1, $2, $3, $4}' m="${model}" /home/cars/

This User Gave Thanks to vgersh99 For This Post:
# 3  
Old 04-25-2012
Quote:
Originally Posted by vgersh99
Code:
awk '$2 ~ m {print $1, $2, $3, $4}' m="${model}" /home/cars/

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
# 4  
Old 04-25-2012
not tested....
Code:
#!/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

This User Gave Thanks to vgersh99 For This Post:
# 5  
Old 04-25-2012
Quote:
Originally Posted by vgersh99
not tested....
Code:
#!/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

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

Code:
#!/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!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Read the line from the last and display the one which is non-zero

Hi All, My requirement is this. Am trying to find a one liner unix command, which will check the last line of the file for 0. If it is zero, it would pick the previous line, else it should display the last line. Eg: abcd efgh ijhk 0 0 Output expected -- ijhk Please share your... (8 Replies)
Discussion started by: hariniiyer300
8 Replies

2. Emergency UNIX and Linux Support

Grab total page read from webalizer display in html

Hi, I need a way to grab the total combines since inception, total pages read from webalizer on my centos server or any other location (as long as since inception) and display the result live on my website So with each visit it would be increasing, or perhaps live (ajax) not sure But can... (0 Replies)
Discussion started by: lawstudent
0 Replies

3. Shell Programming and Scripting

Read File and Display The Count of a particular field

Hi Mates, I require help in the following: I have the following file snmp.txt Wed Mar 2 16:02:39 SGT 2011 Class : mmTrapBladeS origin : 10.0.0.0 hostname : 10.0.0.2 msg : IBM Blade Alert: Calendar Index : 10.0.0.2-IBMBLADE Fri Mar 4 07:10:54 SGT 2011 Class : mmTrapBladeS... (2 Replies)
Discussion started by: dbashyam
2 Replies

4. Shell Programming and Scripting

Read content between xml tags with awk, grep, awk or what ever...

Hello, I trying to extract text that is surrounded by xml-tags. I tried this cat tst.xml | egrep "<SERVER>.*</SERVER>" |sed -e "s/<SERVER>\(.*\)<\/SERVER>/\1/"|tr "|" " " which works perfect, if the start-tag and the end-tag are in the same line, e.g.: <tag1>Hello Linux-Users</tag1> ... (5 Replies)
Discussion started by: Sebi0815
5 Replies

5. UNIX for Dummies Questions & Answers

Using Awk within awk to read all files in directory

I am wondering if anyone has any idea how to use an awk within awk to read files and find a match which adds to count. Say I am searching how many times the word crap appears in each files within a directory. How would i do that from the command prompt ... thanks (6 Replies)
Discussion started by: flevongo
6 Replies

6. UNIX for Dummies Questions & Answers

display whats in an array side by side

ok, so ive got some values that are inputed by a user and are stored in an array. Now what i want to do is change this piece of code below to show ina different way. } for (( num=0; num<9; num++ )) do echo ${arr} done } which outputs the array contents each on a seperate line. I want... (3 Replies)
Discussion started by: strasner
3 Replies

7. Shell Programming and Scripting

awk help..string display

Dear All I had input file as mention below. <RXMFP:MO=RXOTX-46-5; RADIO X-CEIVER ADMINISTRATION MANAGED OBJECT FAULT INFORMATION MO BTSSWVER RXOTX-46-5 ERA-G04-R11-V01 RU RUREVISION RUSERIALNO 0 RUPOSITION ... (1 Reply)
Discussion started by: jaydeep_sadaria
1 Replies

8. UNIX for Advanced & Expert Users

Awk display

Hi All, I got a script like #!/bin/ksh echo "Welcome Sir"|awk ' { print $1 }' This is printing Welcome. I want output like 'Welcome' enclosing by single quotes. Please assist me. Thanks, Shahnaz. (4 Replies)
Discussion started by: shahnazurs
4 Replies

9. Shell Programming and Scripting

How to read variable with no display

I'll be reading user name and password from the person while running a shell script so that he is authenticated. The challenge here is to read the password variable without displaying on screen. Is there a way? I presently do it displaying it on the screen as echo " please enter your... (9 Replies)
Discussion started by: dayanandra
9 Replies

10. Shell Programming and Scripting

whats wrong with this awk???

while read LINE; do echo |awk -v LIN="${LINE}" '{print substr(LIN,1,7)","substr(LIN,8,5)" ,"substr(LIN,14,10)","substr(LIN,24,6)" ,"substr(LIN,30,8)}'; done < exp1.txt exp1.txt = 1234 grgrg 203902 ksdjh oehr weo why it doesn't work?? thanks. (5 Replies)
Discussion started by: george_
5 Replies
Login or Register to Ask a Question