Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Cant assign variable with AWK. Need help

Posted on 2004-10-09
5
Medium Priority
?
381 Views
Last Modified: 2012-05-05
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"
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
  • 3
5 Comments
 
LVL 3

Expert Comment

by:jacauc
ID: 12269222
Why not try :

cat $found
or
echo $found


Thanks
J
0
 
LVL 2

Expert Comment

by:stokesj56
ID: 12281192
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
 

Author Comment

by:elwayisgod
ID: 12283428
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
 
LVL 2

Accepted Solution

by:
stokesj56 earned 750 total points
ID: 12288796
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
 
LVL 2

Expert Comment

by:stokesj56
ID: 12291223
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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Occasionally Windows/Microsoft Updates will fail to update. We have found a code that will delete all temporary files and re-register all dll's related to Windows/Microsoft Updates! This works 99% of the time to get the updates working again! The…
In a recent article here at Experts Exchange (http://www.experts-exchange.com/articles/18880/PaperPort-14-in-Windows-10-A-First-Look.html), I discussed my nine-month sandbox testing of the Windows 10 Technical Preview, specifically with respect to r…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

730 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