Solved

String search in NAWK

Posted on 2004-10-09
3
513 Views
Last Modified: 2006-11-17
Hi,

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?


#!/usr/bin/ksh

# Search by Name
sleep 2
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

LNAME=$lname

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

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

then

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

else

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

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: \n"
read ansnamesrch
case "$ansnamesrch" in
      y*|Y*)  ./name.ksh
            ;;
    *)  
      echo "Retuning to Main Menu: \n"
      sleep 2 ; clear ; ./menu.ksh
      ;;
esac
0
Comment
Question by:elwayisgod
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 21

Accepted Solution

by:
tfewster earned 125 total points
ID: 12268575
Enclose the command in backticks + assign the passed pariable on the awk command line:
found=`nawk -F"," -v LNAME=$lname '$1 == LNAME { print }'  flight.data`

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 flight.data | tr [a-z] [A-Z] > flight.data
- if flight.data is a large file, there's a risk the output will overwrite the file before it's been fully read
Safer to do
cat flight.data | tr [a-z] [A-Z] > flight.data.new && mv flight.data.new flight.data

0
 

Author Comment

by:elwayisgod
ID: 12269136
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 depart_flight.data file, then print those lines to a new file called flight.data, then I do a report etc....


#!/usr/bin/ksh

# 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","$ }' flight.data > flight.data
#cat depart_flight.data
#found=`grep -iw "depcty$" depart_flight.data`

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

0
 
LVL 21

Expert Comment

by:tfewster
ID: 12269164
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 flight.data

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
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how to check for shares on aix 1 111
auto mounter on hp-ux 2 65
check the file dates in unix 14 76
umask commands 5 15
I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
Introduction Regular patching is part of a system administrator's tasks. However, many patches require that the system be in single-user mode before they can be installed. A cluster patch in particular can take quite a while to apply if the machine…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question