NEW: need help with nawk using -v vars

I'm trying to pass nawk a shell variable to be used in a pattern match. I can't get this work.

I'm calling nawk from a /bin/sh

I want that when somebody enters Trunk Group in variable TGR so it goes into nawk variable TG.

echo "Enter TRUNK GROUP:"
read TGR

cat /omp-data/logs/5etr/$SearchDate.APX | nawk -F"|" -v P=$TGR '
#cat /omp-data/logs/5etr/$SearchDate.APX

BEGIN {

       TG=$P;

 printf ("DATE IN ST EN  TGN ISEIZE ISATMP   IANS OATMPT   OVFL OSEIZE OSATMP   OANS TOTUSG OOSMTCE   OOS DBLSZR NTWCONG\n");
}

PLEASE can anyone help me out with this code somehow it does not work..
THANKS in advance

echo "Enter TRUNK GROUP:"
read TGR

nawk -F"|" -v P=$TGR '
BEGIN {
   TG=P;
   printf ("DATE IN ST EN  TGN ISEIZE ISATMP   IANS OATMPT   OVFL OSEIZE OSATMP   OANS TOTUSG OOSMTCE   OOS DBLSZR NTWCONG\n");
}
 . . . . .
' /omp-data/logs/5etr/$SearchDate.APX

Jean-Pierre.

Can you please tell me that do i have to convert variable P into integer in BEGIN. Because now it is running but giving no output.
Actaually value of P should be like 700, 701, 744, 601 like this..

echo "Enter TRUNK GROUP:"
read TGR

cat | nawk -F"|" -v P=$TGR '
BEGIN {
TG=P;
printf ("DATE IN ST EN TGN ISEIZE ISATMP IANS OATMPT OVFL OSEIZE OSATMP OANS TOTUSG OOSMTCE OOS DBLSZR NTWCONG\n");
}
. . . . .
' /omp-data/logs/5etr/$SearchDate.APX

Where is the cat command for?

Regards

I moved the input filename on the awk command, but i forgot to remove the cat command.

cat | nawk -F"|" -v P=$TGR '

Jean-Pierre.

I am trying to make a simple script in which i take input from shell and then forward the value to
nawk (BEGIN).

but when i run below mention script so it give no output.

Code:
echo "Enter TRUNK GROUP:"
read TGR

cat /omp-data/logs/5etr/080422.APX | nawk -F"|" -v P=$TGR '
BEGIN {

TG=P;

printf ("DATE IN ST EN  TGN ISEIZE ISATMP   IANS OATMPT   OVFL OSEIZE OSATMP   OANS TOTUSG OOSMTCE   OOS DBLSZR NTWCONG\n");
}

But when i run below code , so its work fine.

Code:
#echo "Enter TRUNK GROUP:"
#read TGR
#cat /omp-data/logs/5etr/080422.APX | nawk -F"|" -v P=$TGR '

cat /omp-data/logs/5etr/080422.APX | nawk'

BEGIN {

TG=700;

printf ("DATE IN ST EN  TGN ISEIZE ISATMP   IANS OATMPT   OVFL OSEIZE OSATMP   OANS TOTUSG OOSMTCE   OOS DBLSZR NTWCONG\n");
}

Please help me, to run the above code.

Regards

ANYBODY PLEASE REPLY !!!!!!!!

Regards,
Waqas Ahmed

The variable TG is not use in the sample of your awk program.
Show us the full awk program.

Jean-Pierre.

Pls don't bunp-up posts - it's against the Rules.

Below is my complete code, what i want is to take Trunk Group in the input and pass it to nawk (BEGIN). Below mentioned code is not giving any output.

Also see code below which gives perfect output, if i give TG
simple value like TG=700.

KarachiOMP adnan> vi tgcmp602
#!/bin/sh
#
#  Variable TG has to be set according to the trkg desired.
#
#  Also the rop file has to be updated according with the date
#  ander analysis.
#                       ie:
#                       /omp-data/logs/5etr/yymmdd.APX
#
##############################################
# Get the date stamp you want
##############################################
CurrentDate=`date +%y%m%d`
echo "Enter Search Date [$CurrentDate]:"
read SearchDate
if [ "$SearchDate" = "" ]
then
   SearchDate=${CurrentDate}
fi

echo "Enter TRUNK GROUP:"
read TGR

cat /omp-data/logs/5etr/$SearchDate.APX | nawk -F"|" -v P=$TGR  '

BEGIN {

TG=P;
        printf ("DATE IN ST EN  TGN ISEIZE ISATMP   IANS OATMPT   OVFL OSEIZE OSATMP   OANS TOTUSG OOSMTCE   OOS DBLSZR NTWCONG\n");
}

/TRFTR TGCOMP/ {
        getline; getline;
        split($2,a,"-");

        date=a[2]a[3];

        getline; getline; getline;

        interval=$2; split($3,a,":"); start=a[1]; split($4,a,":"); end=a[1];

}

/TGN    ISEIZE/ {

        getline;

        tgn=$1; iseize=$2; isattmp=$5; ians=$6;

}

/TGN    OATTMPT/ {

        getline;

        oattmpt=$2; ovfl=$4; oseize=$5; osattmp=$7;

}

/TGN    OANS/   {

        getline;

        oans=$2; totusg=$3;

}

/TGN    BWOUTU/ {

        getline;

        oosmtce=$4;

        oos=$5;

}

/TGN    DBLSZR/ {

        getline;

        dblszr=$2;

}

/TGN    SBBSY/  {

        getline;

        ntwcong=$4;

}


/TGN    TRKNAV/ {

        if (tgn==TG) {

        printf("%s %2d %2d %2d %4d %6d %6d %6d %6d %6d %6d %6d %6d %6d %6d %6d %6d %6d\n", date, interval, start, end, tgn, iseize, isattmp, ians, oat

        printf("%s %2d %2d %2d %4d %6d %6d %6d %6d %6d %6d %6d %6d %6d %6d %6d %6d %6d\n", date, interval, start, end, tgn, iseize, isattmp, ians, oat
tmpt, ovfl, oseize, osattmp, oans, totusg, oosmtce, oos, dblszr, ntwcong);

        }

}


END {

} '

exit
:q!

Code below which gives perfect output, if i give TG
simple value like TG=700.

KarachiOMP adnan> vi tgcmp602
#!/bin/sh
#
#  Variable TG has to be set according to the trkg desired.
#
#  Also the rop file has to be updated according with the date
#  ander analysis.
#                       ie:
#                       /omp-data/logs/5etr/yymmdd.APX
#
##############################################
# Get the date stamp you want
##############################################
CurrentDate=`date +%y%m%d`
echo "Enter Search Date [$CurrentDate]:"
read SearchDate
if [ "$SearchDate" = "" ]
then
   SearchDate=${CurrentDate}
fi

#echo "Enter TRUNK GROUP:"
#read TGR

cat /omp-data/logs/5etr/$SearchDate.APX | nawk '

BEGIN {

TG=700;
        printf ("DATE IN ST EN  TGN ISEIZE ISATMP   IANS OATMPT   OVFL OSEIZE OSATMP   OANS TOTUSG OOSMTCE   OOS DBLSZR NTWCONG\n");
}

/TRFTR TGCOMP/ {
        getline; getline;
        split($2,a,"-");

        date=a[2]a[3];

        getline; getline; getline;

        interval=$2; split($3,a,":"); start=a[1]; split($4,a,":"); end=a[1];

}

/TGN    ISEIZE/ {

        getline;

        tgn=$1; iseize=$2; isattmp=$5; ians=$6;

}

/TGN    OATTMPT/ {

        getline;

        oattmpt=$2; ovfl=$4; oseize=$5; osattmp=$7;

}

/TGN    OANS/   {

        getline;

        oans=$2; totusg=$3;

}

/TGN    BWOUTU/ {

        getline;

        oosmtce=$4;

        oos=$5;

}

/TGN    DBLSZR/ {

        getline;

        dblszr=$2;

}

/TGN    SBBSY/  {

        getline;

        ntwcong=$4;

}


/TGN    TRKNAV/ {

        if (tgn==TG) {

        printf("%s %2d %2d %2d %4d %6d %6d %6d %6d %6d %6d %6d %6d %6d %6d %6d %6d %6d\n", date, interval, start, end, tgn, iseize, isattmp, ians, oat

        printf("%s %2d %2d %2d %4d %6d %6d %6d %6d %6d %6d %6d %6d %6d %6d %6d %6d %6d\n", date, interval, start, end, tgn, iseize, isattmp, ians, oat
tmpt, ovfl, oseize, osattmp, oans, totusg, oosmtce, oos, dblszr, ntwcong);

        }

}


END {

} '

exit
:q!

Please Help...

Regards,
Waqas Ahmed

For debugging purpose, add some display to check to value of your variables.

echo "Enter TRUNK GROUP:"
read TGR

echo "TGR=<$TGR>"

cat /omp-data/logs/5etr/$SearchDate.APX | nawk -F"|" -v P=$TGR  '

BEGIN {

TG=P;
printf("AWK - P=<%s> String_TG=<%s> Number_TG=%d\n", P, TG, TG);

        printf ("DATE IN ST EN  TGN ISEIZE ISATMP   IANS OATMPT   OVFL OSEIZE OSATMP   OANS TOTUSG OOSMTCE   OOS DBLSZR NTWCONG\n");
}

Jean-Pierre.

For debugging purpose, I add some display to check values of variables.

KarachiOMP adnan> ./tgcmp602
Enter Search Date [080426]:
080425
Enter TRUNK GROUP:
700
AWK - P=<700> String_TG=<700> Number_TG=700
DATE IN ST EN TGN ISEIZE ISATMP IANS OATMPT OVFL OSEIZE OSATMP OANS TOTUSG OOSMTCE OOS DBLSZR NTWCONG

The value in the variable is going correctly, but i still dont know that why desired output is not coming.

Below is code, which gives perfect output

cat /omp-data/logs/5etr/$SearchDate.APX | nawk '

BEGIN {

TG=700;

    printf \("DATE IN ST EN  TGN ISEIZE ISATMP   IANS OATMPT   OVFL OSEIZE OSATMP   OANS TOTUSG OOSMTCE   OOS DBLSZR NTWCONG\\n"\);

}

But i dont know why this simple script dont give output.

echo "Enter TRUNK GROUP:"
read TGR

echo "TGR=<$TGR>"

cat /omp-data/logs/5etr/$SearchDate.APX | nawk -F"|" -v P=$TGR '

BEGIN {

TG=P;
printf("AWK - P=<%s> String_TG=<%s> Number_TG=%d\n", P, TG, TG);

    printf \("DATE IN ST EN  TGN ISEIZE ISATMP   IANS OATMPT   OVFL OSEIZE OSATMP   OANS TOTUSG OOSMTCE   OOS DBLSZR NTWCONG\\n"\);

}

ANY IDEA..

PLZ HELP !!!

Regards,
Waqas Ahmed

Why don't you define the field separator FS in same manner in the two cases ?

When you the value to 700, the field separator is omited, so its value is space.

cat /omp-data/logs/5etr/$SearchDate.APX | nawk '

When you use the TGP variable, the FS is set to '|' :

cat /omp-data/logs/5etr/$SearchDate.APX | nawk -F"|" -v P=$TGR '

Jean-Pierre.

I think you are asking that why i define field separator FS like this:-F"|" -v P=$TGR ' in the script which is not working .

cat /omp-data/logs/5etr/$SearchDate.APX | nawk -F"|" -v P=$TGR '

so sir actually i am modifying my old script which is this:

cat /omp-data/logs/5etr/$SearchDate.APX | nawk '
BEGIN {
TG=700;
printf ("DATE IN ST EN TGN ISEIZE ISATMP IANS OATMPT OVFL OSEIZE OSATMP OANS TOTUSG OOSMTCE OOS DBLSZR NTWCONG\n");
}

the problem with this script is this , that every time we have to open the script file with vi and change the value of TG like 700,701, 702 etc..which is not easy for normal user. so i just want to modify the script so it takes input of TG at the startup and gives the output.

For debugging purpose we have seen that P variable is getting right value.
but i dont know why its not working.

KarachiOMP adnan> ./tgcmp602
Enter Search Date [080426]:
080425
Enter TRUNK GROUP:
700
AWK - P=<700> String_TG=<700> Number_TG=700
DATE IN ST EN TGN ISEIZE ISATMP IANS OATMPT OVFL OSEIZE OSATMP OANS TOTUSG OOSMTCE OOS DBLSZR NTWCONG

Please share if you have some other idea in mind to solve the problem.

Regards,
Waqas Ahmed

But why did you add the -F"|" option, it's a completely unrelated change; does it work if you take it out?

Thanks alot Mr. Jean-Pierre and era for your KIND HELP.

The problem is solved now. -F"|" was the problem.

Correct CODE:

echo "Enter TRUNK GROUP:"
read TGR

echo "TGR=<$TGR>"

cat /omp-data/logs/5etr/$SearchDate.APX | nawk -v P=$TGR '

BEGIN {

TG=P;
printf("AWK - P=<%s> String_TG=<%s> Number_TG=%d\n", P, TG, TG);

printf ("DATE IN ST EN TGN ISEIZE ISATMP IANS OATMPT OVFL OSEIZE OSATMP OANS TOTUSG OOSMTCE OOS DBLSZR NTWCONG\n");
}

Now i hope my boss will impress with me.. Lets see:)

Regards,
Waqas Ahmed

Perhaps now would be a good time then to revisit the issue related to the Useless Use of Cat Award.

cat file | awk is an antipattern; the cat does not add any benefit, and costs you a process. It's better style to simply say awk file

Ok now i require more help !

Now i want that when my script gives the output so it ask with me at the end to exit or not , if i enter y so it exits and if i enter n so it starts once again.

Please explain how to do this...

Regards,
Waqas Ahmed

while true; do
  yourscripthere
  echo -n "Play again? [y/N] "
  read reply
  case $reply in [Yy]*) ;; *) break;; esac
done

That's an endless loop; you break out of it by not replying "y" (or yes, or yowza, or ypsilon) at the prompt.

echo -n is not completely portable; see the manual page for your shell, or your echo command, to see what works at your site. (Might be 'echo "Play again? [y/N] \c"' too. Many shells have echo built into the shell, but most sites also have a /bin/echo in case some users have an old shell which doesn't have its own echo command. You need to figure out which one works for you.)

Thanks alot ERA , its working!!

Can you explain this line, especially this ;; *) break;; esac

case $reply in [Yy]*) ;; *) break;; esac

what exactly this line is doing ?

Regards,
Waqas Ahmed