AWK programming error

Asked by adithya

there is a database program i am trying to execute i.e. shell programming but its giving me an error while using AWK instructions..i.e. a syntax error..i am unable to remove it..i hope you can help me..

Question information

Language:
English Edit question
Status:
Answered
For:
Ubuntu gnome-terminal Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
adithya (24adithya) said :
#1

also ,i would like to know more information related to awk programming..i.e. in depth knowledge of it..and wat package can be useful for it..

Revision history for this message
adithya (24adithya) said :
#2

i am also sending an attachment

Revision history for this message
adithya (24adithya) said :
#3

the error is as follows for 'inserting' i.e. case 3..
Menu
1.Create
 2.Display
 3.Insert
 4.Delete
 5.Modify
 6.Search
 7.Sort
 8.Result
 9.Exit

Enter your choice
3
entr rno..
2
awk: line 1: syntax error at or near if
bash: test: flag: integer expression expected

i am also sending the source code below..

ch=1
while test $ch -ne 0
do
    echo -e "Menu"
 echo -e "1.Create\n 2.Display\n 3.Insert\n 4.Delete\n 5.Modify\n 6.Search\n 7.Sort\n 8.Result\n 9.Exit\n"
 echo -e "Enter your choice"
 read ch

 case $ch in

  1)echo "enter name"
  read name
  echo "enter roll no"
  read rno
  echo "enter department"
  read dept
  echo "enter marks 1"
  read m1

   while test $m1 -gt 100
   do
   echo "enter marks 1"
   read m1
   done
  echo "enter marks 2"
  read m2

   while test $m2 -gt 100
   do
   echo "enter marks 2"
   read m2
   done
  echo "enter marks 3"
  read m3

   while test $m3 -gt 100
   do
   echo "enter marks 3"
   read m3
   done

  echo -e "$rno\t$name\t$dept\t$m1\t$m2\t$m3">DATA
  ;;

  2)cat DATA
  ;;

  3) flag=1
     while test $flag -ne 0
     do
    flag=0
    echo "entr rno.."
    read rno

    awk 'if(rno==$1)
     {
      print $1>"tem"
       }'DATA

    if test -f tem
      then
     read flag<tem
     rm -f tem
    fi
   done
    if test flag -ne 1
      then
        echo -e "$rno">>DATA
    fi
  ;;
  9)exit
  ;;

 esac
done

Revision history for this message
Helmut Koeberle (helmut.koeberle) said :
#4

Could you try the following:

Replace
    awk 'if(rno==$1)
     {
      print $1>"tem"
       }'DATA
with
    awk -v rno=$rno ' rno==$1
     {
      print $1>"tem"
       }' DATA

Revision history for this message
Tony Pursell (ajpursell) said :
#5

Or better still

awk -v rno=$rno 'rno == $1 {print $1 > "tem"}' DATA

i.e no 'if' needed

also, further down, change

if test flag -ne 1

to

if test $flag -ne 1

That removes errors, but I still do not see what you are trying to do!

For more information, see

man awk

or just Google for awk. There are lots of examples, tutorials, etc out there!

Tony

Revision history for this message
adithya (24adithya) said :
#6

wel,Tony it din't solve my problem..while inserting i am checking whether
the same roll no exists which is being entered by the user..creation and
displaying works but insertion is giving a problem.Thanks for your help
though.

Revision history for this message
Tony Pursell (ajpursell) said :
#7

Hi

Try this for inserting

  3) flag=1
     while test $flag -ne 0
     do
    flag=0
    echo "entr rno.. (or 0 to end Inserting)"
    read rno
    if test $rno -ne 0
    then
    if test `grep -c "^$rno\b" DATA` -eq 0
 then
          echo "enter name"
   read name
   echo "enter department"
   read dept
   echo "enter marks 1"
   read m1

    while test $m1 -gt 100
    do
    echo "enter marks 1"
    read m1
    done
   echo "enter marks 2"
   read m2

    while test $m2 -gt 100
    do
    echo "enter marks 2"
    read m2
    done
   echo "enter marks 3"
   read m3

    while test $m3 -gt 100
    do
    echo "enter marks 3"
    read m3
    done

   echo -e "$rno\t$name\t$dept\t$m1\t$m2\t$m3">>DATA
   flag=1
     else
 echo "$rno exists ... try again"
        flag=1
     fi
     else
 flag=0
     fi
     done
  ;;

Tony

Can you help with this problem?

Provide an answer of your own, or ask adithya for more information if necessary.

To post a message you must log in.