String search in NAWK


I'm trying to have user type in a name to search for, then have it read the variable.  I need it to match exactly, thus cant use grep as they would get results by typing in 'smit' when 'smith' exists in database. Below is my nawk command. Any ideas where I'm going wrong?


# Search by Name
sleep 2
cat | tr [a-z] [A-Z] > #Change data file to UPPERCASE

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


#nawk -F"," -v NAME=`cat lnametr` '$ 1== NAME { print }' >
found=nawk -F"," -v '$ 1 == LNAME { print }' >

#found=`grep -i "^$lname"`
if [ "$found" != "" ]


echo "\n\nName Found \n\n"


echo "\n\nThe Last Name" $lname "was not found \n\n
         Returning you to Last Name search  \n" ; ./name.ksh


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

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

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


case "$srtfld" in

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

echo "\n\nWould you like to do another search by Last Name: \n"
read ansnamesrch
case "$ansnamesrch" in
      y*|Y*)  ./name.ksh
      echo "Retuning to Main Menu: \n"
      sleep 2 ; clear ; ./menu.ksh
Who is Participating?
tfewsterConnect With a Mentor Commented:
Enclose the command in backticks + assign the passed pariable on the awk command line:
found=`nawk -F"," -v LNAME=$lname '$1 == LNAME { print }'`

Incidentally, it would help if you said what the problem or error was and just post the code snippet that is giving you trouble ;-)

BTW, I suspect you might run into problems with
cat | tr [a-z] [A-Z] >
- if is a large file, there's a risk the output will overwrite the file before it's been fully read
Safer to do
cat | tr [a-z] [A-Z] > && mv

elwayisgodAuthor Commented:
Cant get it to assign found correctly. It just hangs on the last cat statement.  I'm stuck. All help is appreciated.  What I'm trying to do put in a variable in any case and if it matches exactly to the first field in the file, then print those lines to a new file called, then I do a report etc....


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

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

nawk -F"," '{ print $3","$1","$2","$4","$5","$6","$ }' >
#found=`grep -iw "depcty$"`

echo "$depcty" | tr [a-z] [A-Z] > depctytr
found=`nawk -F"," -v DEPCITY=depctytr '$1 == DEPCITY { print }'`
cat $found

echo "$depcty" | tr [a-z] [A-Z] > depctytr              # Creates a _file_ called "depctytr" containing the output of the echo command.
I suspect you want
depctytr=`echo "$depcty" | tr [a-z] [A-Z]`

found=`nawk -F"," -v DEPCITY=depctytr.........       # passes the _string_ "depctytr" to awk; You want $depctytr, i.e. the contents of the _variable_ depctytr.  As it is, $found will be the empty string as the string "depctytr" is NOT in

cat $found                  # cat the _file_  whose name matches the contents of $found, e.g. LAX;  Is this what you wanted?  However, due to the errors above, $found="" so `cat` waits for input
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.