AWK syntax help

i have a ksh code that needs to be written in AWK. can someone please help me here? :frowning:

 
if [ $(print - "${ignore[@]}" | grep -c "$2") -gt 0 ] ; then 
print - "found $2 in array ignore[]" 
else 
print - "did not find $2 in array ignore[]" 
fi

ignore[0]=4ty56r
ignore[1]=er45ty
.
.
.
ignore[n]=frhtg2

n is a variable.

Better if you post the array content/input data.

ignore[0]=4ty56r
ignore[1]=er45ty
.
.
.
ignore[n]=frhtg2

n is a variable.

print - "${ignore[@]}" | awk -v searchStr="$2" ' searchStr { print "found $2 in array ignore[]";flag=1; exit }
END { if ( flag != 1 ) { print "did not find $2 in array ignore[]" } } '

did you happen to test this code? it is complaining about `(' unexpected in 2nd line.

Its working for me.

Can you show the trace of your run?

what do you mean by trace? This is what i see in the command prompt.

flipper> roboSheriff.ksh
../sbin/roboSheriff.ksh[29]: syntax error at line 98 : `(' unexpected

51    endMarker="NSC Termination Event Details:"
    52
    53    if [ ! -f $4 ] ; then
    54      echo "No $4 file found.  Perhaps testMgr is still running?"
    55
    56    else
    57
    58      # Subtract out a little from the start time.  Otherwise we miss
    59      # crashes because of the controller clocks being off by a few seconds.
    60      #dateFilter $4 $(( $2 - 10 )) $3 > $con
    61
    62      # BGL -- analyze all of the consoles.  Autotest does the trimming now.
    63
    64      termReport.pl < $4 | dos2ux |  head -2000 | awk '
    65      BEGIN { foundTermEvent=0;
    66              endMarker="'"${endMarker}"'"; }
    67
    68      {
    69        if ( (substr($0, 1, length(endMarker)) == endMarker) ||
    70             (substr($0, 1, 39) == "'${SBINDIR}'/termReport.pl: ") )
    71        {
    72          if (foundTermEvent == 1)
    73          {
    74            print "";
    75            foundTermEvent=0;
    76          }
    77        }
    78        ##############################################################################
    79         if (foundTermEvent == 1)
    80         {
    81                  # if ( $(print - "${ignore[@]}" | grep -c "$2") -gt 0 ) ; then
    82                  # print - " FOUND $2 IN ARRAY IGNORE[] "
    83           #       { print $1 }
    84           #       { print $2 }
    85           #        foundTermEvent = 0;
    86           #       print " Z= " z
    87           #       print " ignore[$z] = " ignore[z]
    88           #       print " ignore[$z] = " ignore[z]
    89
    90                  # else
    91                   #print;
    92                  # print - " DID NOT FIND $2 IN ARRAY ignore[ ] "
    93                   #{ print $2 }
    94                   #print " ########################## "
    95                  # fi
    96
    97                  print - "${ ignore[@] }" | awk -v searchStr="$2" ' searchStr { print "found $2 in array ignore[]";flag=1; exit }
    98                  END { if (flag != 1) { print "did not find $2 in array ignore[]" } } '
    99
   100         }
   101  ####################################################################
   102        if (substr($0, 1, 18) == "Termination Event:")
   103        {
   104          foundTermEvent=1;
   105        }
   106      }'
   107  
   108    fi
   109  }
   110  
   111  
   112  function analyzeFailure

Add set -vx in your script and then run to get the trace.

i think the issue is i am already in a awk script, so it doesnt like me putting another awk, dont you think?

You are right. You didnt explain in your post where you are going to replace your grep code.

Try this

ignre=${ignore[@]}
termReport.pl < $4 | dos2ux | head -2000 | awk -v ignre="${ignre}" -v searchStr="$2" '  BEGIN { foundTermEvent=0; 66 endMarker="'"${endMarker}"'"; } 
............
#{ print $2 } 
#print " ########################## " 
# fi

if ( ignre ~ searchStr ) { print "found $2 in array ignore[]" }
else { print "did not find $2 in array ignore[]" 

That cause data limitation issue. Here is what i see.

What is the value of variable SCID_FM_AL ?

Can you show your modified script?

Are you asking about SCID_FM_AL ? that is not even in this script. i am not sure how big that is. Here is the code.

---------- Post updated at 04:18 PM ---------- Previous update was at 04:05 PM ----------

[/COLOR]All i am trying to do is to see if $2 (which i already read fine), is in my array(i also read fine). i have been working on this part for more than a day now and this isnt going any where good. May be i should do a while loop and see if i can do that. What do you think?

There is a limitation of 399 bytes in AIX for length of Regular expression and literal strings.

Run this code outside awk and pass status to awk and print it based on foundTermEvent

if [ $(print - "${ignore[@]}" | grep -c "$2") -gt 0 ] ; 
then 
   status="found $2 in array ignore[]" 
else 
   status="did not find $2 in array ignore[]" 
fi

i dont think i understood that correctly. Please check my code to see if this is what you meant. by the way, thank you very much for helping me. i was out of options.

 file=../data/globallyIgnoredTc.dat
   #set -A ignore `grep -v "#" ${file} | tr '[A-Z]' '[a-z]' `
   set -A ignore `grep -v "#" ${file} `
   ign=${ignore[@]}
       #################################################

  endMarker="NSC Termination Event Details:"

  if [ ! -f $4 ] ; then
    echo "No $4 file found.  Perhaps testMgr is still running?"

  else

    # Subtract out a little from the start time.  Otherwise we miss
    # crashes because of the controller clocks being off by a few seconds.
    #dateFilter $4 $(( $2 - 10 )) $3 > $con

    # BGL -- analyze all of the consoles.  Autotest does the trimming now.
    
	if [ $(print - "${ignore[@]}" | grep -c "$2") -gt 0 ] ; 
		then 
			status="found $2 in array ignore[]" 
		else 
			status="did not find $2 in array ignore[]" 
	fi



    termReport.pl < $4 | dos2ux |  head -2000 | awk '
    
    BEGIN { foundTermEvent=0;
            endMarker="'"${endMarker}"'"; }

    {
      if ( (substr($0, 1, length(endMarker)) == endMarker) ||
           (substr($0, 1, 39) == "'${SBINDIR}'/termReport.pl: ") )
      {
        if (foundTermEvent == 1)
        {
          print "";
          foundTermEvent=0;
        }
      }
      ##############################################################################
       if (foundTermEvent == 1)
       {
				  if (status == "found $2 in array ignore[]")
				  {
                  foundTermEvent = 0;
				  }

                  else
                  {
				  print;
				  }
   

       }
####################################################################
      if (substr($0, 1, 18) == "Termination Event:")
      {
        foundTermEvent=1;
      }
	  }'
	  fi
	  }

You are right. Did the script run fine now?

i dont get any errors now. but it is not doing what it supposed to do. it seems like it never finds a match when comparing $2 to the array. in reality, there are matches and it should find it.

could this happen because we have if statement before i have any data for $2?

---------- Post updated at 01:04 PM ---------- Previous update was at 12:46 PM ----------

i verified this, it doesnt have correct values for $2 by the time if statement excuted. Because $2 values arent going to be ready till AWK statemnet has been excuted.

Is $2 second argument to your script or Second field in your input from termReport.pl?

$2 is comming from termReport.pl out put. it is the 2nd field.

Create another script to check whether second field exists in ignore array

checkSecondFldInArray

file="path_to_file"
set -A ignore `grep -v "#" ${file} ` 

if [ $(print - "${ignore[@]}" | grep -c "$1") -gt 0 ] ; 
then 
	echo "found" 
else 
	echo "not found" 
fi

Change awk code to call above script

if (foundTermEvent == 1) 
{ 
	"checkSecondFldInArray " $2 | getline status
	if (status == "found") { foundTermEvent = 0; }