Cant assign variable with AWK. Need help

I can't get the found variable to work. I need to use awk as I need an exact match with my variable $depcty. The last command errors out with
cat: cannot open



# Search by Departure city
clear
cat flight.data | tr [a-z] [A-Z] > flight.data #Change data file to UPPERCASE

echo "Enter the 3 character Departure City airport code to search for: \n"
read depcty
sleep 2
clear

nawk -F"," '{ print $3","$1","$2","$4","$5","$6","$7","$3 }' flight.data > new_flight.data
cat "new_flight.data"
#cat depart_flight.data
#found=`grep -iw "depcty$" depart_flight.data`

echo "$depcty" | tr [a-z] [A-Z] > depctytr
cat "depctytr"
found=`nawk -F"," -v DEPCITY=depctytr '$1 == DEPCITY { print }'  new_flight.data`
cat "$found"
elwayisgodAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
stokesj56Connect With a Mentor Commented:
Your redirect at the end of the nawk statement puts the output of the BEGIN printf statement into your output file (newflight.data). The hang up is waiting for your input. One way around this is to do it like this:

        PROMPT="Enter the 3 character Departure City airport code to search for:"
        system("echo " PROMPT ">&2")

This sends the prompt to Errout (&2). Alternately you can do the redirect inside the nawk script:

nawk -F"," '
    BEGIN {
        printf("Enter the 3 character Departure City airport code to search for: ")
        getline DEPCITY < "/dev/tty"
        DEPCITY=toupper(DEPCITY)
        system("cat /dev/null >newflight.data")
    }
    { $1=toupper($1) }
    $1==DEPCITY { print $3","$1","$2","$4","$5","$6","$7","$3 >>"newflight.data" }
' flight.data
# End

This would allow for multiple line to matched in case you have more than 1 line in your input file (flight.data) with the same 3 letter airport code. If you only want the first match then you can change it to:

nawk -F"," '
    BEGIN {
        printf("Enter the 3 character Departure City airport code to search for: ")
        getline DEPCITY < "/dev/tty"
        DEPCITY=toupper(DEPCITY)
    }
    { $1=toupper($1) }
    $1==DEPCITY {
        print $3","$1","$2","$4","$5","$6","$7","$3 >"newflight.data"
        exit
    }
# End
' flight.data
# End

This should run a little quicker in that it exits as soon as a match is found. The previous method would go through the entire input file before exiting.
0
 
jacaucCommented:
Why not try :

cat $found
or
echo $found


Thanks
J
0
 
stokesj56Commented:
Try this:

#!/bin/ksh                                                                                
# Search by Departure city
nawk -F"," '                                                                              
    BEGIN {
        printf("Enter the 3 character Departure City airport code to search for: ")
        getline DEPCITY < "/dev/tty"
        DEPCITY=toupper(DEPCITY)
    }
    { $1=toupper($1) }
    $1==DEPCITY { print $3","$1","$2","$4","$5","$6","$7","$3 }
' flight.data
# End

As long as you are using nawk let it do all the work in a single command.
0
 
elwayisgodAuthor Commented:
When I run this, it just hangs. It does not even do the print statement in the BEGIN part of the nawk. I'm stumped. Any help is appreciated.

sam


#!/usr/bin/ksh



# Search by Last Name
clear
cat flight.data | tr [a-z] [A-Z] > flight.data #Change data file to UPPERCASE

#echo "What Last Name do you want to search for: \n"
#read lname
sleep 2
clear

nawk -F"," '

      BEGIN {
      printf("Enter the last name you want to search for.")
      getline GETNAME < "/dev/tty"
      GETNAME=toupper(GETNAME)
      }
      { $1=toupper($1) }
      $1==GETNAME { print $1","$2","$3","$4","$5","$6","$7 }' flight.data > newflight.data



#nawk -F"," '{ print $1","$2","$3","$4","$5","$6","$7","$1 }' flight.data > newflight.data
#echo "$lname" | tr [a-z] [A-Z] > lnametr

found=$1

if [ "$found" != "" ]

then

echo "\n\n"Name" $1 "Found" \n\n"

else

echo "\n\nThe Last Name" $1 "was not found \n\n
Would you like to continue searching by last name?\n"

read contsrch

case "$contsrch" in

y*|Y*)
echo "\nReturning to last name search." ; sleep 2 ; clear ; ./name.ksh
;;
*)
echo "\nReturning to main menu" ; sleep 2 ; clear ; ./menu.ksh
;;

esac

fi



echo "$lname" | tr [a-z] [A-Z] > lnametr

NAME=`cat lnametr`
nawk -F"," -v NAME=`cat lnametr` '$ 1== NAME { print }'  flight.data > newflight.data

echo "\n\nWhat field would you like to sort your results by: \n\n
       1) Last Name \n
       2) First Name \n
       3) Departure City \n
       4) Arrival City \n
       5) Flight Number \n
       6) Departure Date \n"


read srtfld

clear

case "$srtfld" in

       1)sort +0 -t, newflight.data > lstname.rpt ; ./lstname.report
               ;;
       2)sort +1 -t, newflight.data > lstname.rpt ; ./lstname.report
               ;;
       3)sort +2 -t, newflight.data > lstname.rpt ; ./lstname.report
               ;;
       4)sort +3 -t, newflight.data > lstname.rpt ; ./lstname.report
               ;;
       5)sort +4 -t, newflight.data > lstname.rpt ; ./lstname.report
               ;;
       6)sort +6 -t, newflight.data > lstname.rpt ; ./lstname.report
               ;;
       *) echo "\007\007\007 Invalid Option.  Sorting by default 'Last Name' \n
"
                sort +0 -t, newflight.data > lstname.rpt ; ./lstname.report
               ;;

esac

echo "\n\nWould you like to do another search by Last Name: Y(y) or N(n) \n"
read ansnamesrch
case "$ansnamesrch" in
       y*|Y*)  ./name.ksh
               ;;
   *)
       echo "Returning to Main Menu: \n"
       sleep 2 ; clear ; ./menu.ksh
       ;;
esac

0
 
stokesj56Commented:
Your line above:

found=$1

What are you trying to set the found variable to? It appears that you are passing a variable to the script you have this in. Looking at the script I'm guessing you want the first field of the nawk output. In that case use this line to set the found variable:

found=`cut -d',' -f 1 newflight.data`

Another comment conserning your first tr statement. I'm not sure in it is safe to write to the same file you are reading from. I'd feel more comfortable writting to a temorary file instead of overwritting your original.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.