Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

awk newbies 1

Status
Not open for further replies.

shahrir

Programmer
Jun 3, 2003
6
MY
switch ("$choice")
case "[Aa]":

echo -n "Please Enter The Car Model : "
set input=&quot;$<&quot;
gawk '/$input/ {print &quot;Car Model: &quot;$2;print &quot;Chassis No:
&quot;$1;age=(2003-$4);print &quot;Age of the car: &quot;age}' carfile

echo &quot;&quot;
echo -n &quot;Do You Want To Go Back To Main Menu [Y/N] : &quot;
set choice=&quot;$<&quot;
if ($choice == &quot;Y&quot; || $choice == &quot;y&quot;) then
set flag=true
else
set flag=false
endif

Hi guys...need u guys help on this, i just started learning linux and using awk..i dunno how to call the variables from the script to awk..anyone can help me? please, kinda desperate as this must finish before friday
 
Try

gawk -v inp=$input '$0~inp {print &quot;Car Model: &quot;$2;print &quot;Chassis No:
&quot;$1;age=(2003-$4);print &quot;Age of the car: &quot;age}' carfile


CaKiwi

&quot;I love mankind, it's people I can't stand&quot; - Linus Van Pelt
 
tq 4 the source CaKiwi eventhough i havent try it yet ;)this comes to another question...how should i make this program ignore lower or upper cases? I've read about the IGNORECASE but how to use it?
 
hi guys, sorry if i posted so many questions in a single topics...CaKiwi i've try your code but the result is unmatched..
the basic idea of creating the system is to display the car chasis no,model and age by entering either car model,year or age...
i try to use grep and filter it with awk but the result is still unmatched
here's the code that i've tried...
grep -i '$input' carfile | awk '{print $2}'

any suggestion guys?
 
> i've try your code but the result is unmatched..
Meaning what?
- There is an error in the script - say mis-matched {} pairs?
- Or it failed to find a matching car (script ran ok, just didn't find anything)

Paste a section of the carfile, and the input you typed in which should have matched something in that section.

> I've read about the IGNORECASE but how to use it?
gawk 'BEGIN { IGNORECASE=1 } ....
 
i think i have the solution already..using grep and pipe with awk
grep -i '$input' carfile | awk '{print $2}' ..u were rite salem i did have a mismatched {}
but the problem is (as im still new in linux) how to create an error msg if the user enters an input that didn't match any of the section in the carfile as grep sometimes do some funny things
u guys really helping..
KNOWLEDGE BELONGS TO THE WORLD!!
 
All the standard unix tools return an exit status telling you how successful the command was, so you can take appropriate action.
Code:
grep -i '$input' carfile | awk '{print $2}'
if [ $? -ne 0 ]; then echo nothing found; fi
 
i don't really understand that coding Salem..but i try this and its works..
on top of script i declare a var like this
set readGrep = `grep -i $input carfile`
if ( &quot;$readGrep&quot; == &quot;&quot;) then
echo &quot;invalid data&quot;
else
grep -i $input carfile | ......(as the script above)
But as grep is about finding matching string so even if i enter &quot;12&quot; as there is a line in the carfile that have &quot;12&quot; on it it will display the the field that have the line &quot;12&quot;..so its not really bugs free..any suggestion Salem/CaKiwi/guys? perhaps using FIND but don't ask me :)
 
Well if $input is meant to match a specific field in carfile, then you have to go back to your CaKiwi's original suggestion
Code:
# find all $input which match field 2 in the carfile
gawk -v inp=$input '$2 ~ inp {
  print &quot;Car Model: &quot; $2
  print &quot;Chassis No:&quot; $1
  age=(2003-$4)
  print &quot;Age of the car: &quot; age
}' carfile
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top