AWK help. how to compare a variable with a data array in AWK?

Hi all,

i have a data array as follows.

array[0]=ertfgj2345
array[1]=456ttygkd
.
.
.
array[n]=errdjt3235
 

so number or elements in the array can varies depending on how big the data input is.

now i have a variable, and it is $1 (there are $2, $3 and so on, i am only interested in $1).

in K shell my code would be as follows,

z=0
while [[ $z -lt ${array[*]} ]] ; do
   if [[ ${array[$z]} == "$1" ]]
   then
   print " found a match from list "
   let z=$z+1
 
   else
   print " no match "
   let z=$z+1
   fi
done

This works fine in Kshell. However i am editing a scritp that has been written in kshell and AWK. So the part i am working on is written in AWK and AWK doesnt like my while loop. Can someone help me with this please? :frowning:

$ cat Match
array[0]=ertfgj2345
array[1]=456ttygkd
array[2]=errdjt3235

printf "%s\n" "${array[@]}" | awk '$0 == "'"$1"'" { C++ } END { print C?"Match":"No Match" }'


$ ./Match 456ttygkd
Match

$ ./Match blah
No Match

It's easier in awk, can you post an example of input files?

If you read from two CSV files, then the script it's something like this

awk -F, 'NR==FNR{array[$1];next}{if ($1 in array){print "Found a match"}else{print "No match"}}' fileForArray.csv anotherFile.csv

This script has 2 funtions. i will post the first part of the code.

 
 
#! /usr/bin/ksh

# This script goes through failure logs and prints out a useful
# synopsis of the last failure  
# It relies on test log files, console logs, and autotest.result.
. lib.ksh
if [ _$1  = _-all ] ; then
  all=1
fi  
failures=/tmp/failures.tmp
failLines=/tmp/failLines.tmp
utilFailLines=/tmp/utilFailLines.tmp
miscompareLines=/tmp/miscompareLines.tmp
con=/tmp/con.tmp
failInfo=/tmp/failInfo.tmp
function analyzeConsole
{
  # First, cut the console data down to just the part that printed
  # out during that test.  
  # Then run it through termReport.pl.
  # Then get the interesting parts of that.
  #################################################
     # Reading from globallyIgnoredTc list and setting up an array & converting upper case to lower case
file=../data/globallyIgnoredTc.dat
   set -A ignore `grep -v "#" ${file} | tr '[A-Z]' '[a-z]' `
       #################################################
  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.
    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)
       {
      z=0
          while [[ $z -lt ${#ignore
[*]} ]] ; do     
               if [[ ${ignore[$z]} == "$1" ]]  #I want all my array elements to compair against $1. If $1 match with ANY array element, condition is true.
               then
               print " FOUND A BADTC FROM THE LIST "
               foundTermEvent = 0;
               print " Z=  $z "
               echo " ignore[$z] = ${ignore[$z]}"
               let z=$z+1
 
            else
            #print;
            print " DIDNT FIND ANY BAD TCs "
            print " Z=  $z "
            let z=$z+1
            fi
    done
      }
####################################################################
      if (substr($0, 1, 18) == "Termination Event:")
      {
        foundTermEvent=1;
      }
    }'
  fi
}

function analyzeFailure
{
 

---------- Post updated at 07:14 PM ---------- Previous update was at 07:11 PM ----------

part i get an error is last while loop and the if statement in the while loop. it is complaining about AWK syntax not being correct.

Your whole while loop (and everything in it) is an odd mix of shell and awk. It should be awk.

i know. Before i add the while loop, it used to work error free. i had to add this extra code. How do i convert this new part to AWK?

I must stress that I didn't test this (!)

# Current (Shell)
z = 0
while [[ $z -lt ${#ignore[*]} ]] ; do
  if [[ ${ignore[$z]} == "$1" ]]; then
    print " FOUND A BADTC FROM THE LIST "
    foundTermEvent = 0;
    print " Z= $z "
    echo " ignore[$z] = ${ignore[$z]}"
    let z=$z+1
  else
    print " DIDNT FIND ANY BAD TCs "
    print " Z= $z "
    let z=$z+1
  fi
done

# New (awk)
# Change the line:
# eport.pl < $4 | dos2ux | head -2000 | awk '

# To:
# eport.pl < $4 | dos2ux | head -2000 | paste -sd\| | awk -v S="$1" '

# And the while-loop to:
z = 1
IGNORE = split( $0, IGNORE, "|" )
while( z <= length( IGNORE ) ) {
  if( IGNORE[z] == S ) {
    print " FOUND A BADTC FROM THE LIST "
    foundTermEvent = 0;
    print " Z= " z
    print "IGNORE[z] = " IGNORE[z]
  } else {
    print " DIDNT FIND ANY BAD TCs "
  }
  z++
}

Is there a typo in line? It is complaining about usage in paste

# eport.pl < $4 | dos2ux | head -2000 | paste -sd\| | awk -v S="$1" '

Yes, sorry.

should be

paste -sd \| -

now i am getting an error saying paste: : can not open the file.

---------- Post updated at 09:14 AM ---------- Previous update was at 09:06 AM ----------

ok your code is good. so that line now looks like this,
# eport.pl < $4 | dos2ux | head -2000 | paste -sd\| - | awk -v S="$1" '

Issue is, i get a message saying "awk:input line | found /file/path cannot be longer than 3000 bytes."
"source line number is 3"

Can you help me with this please?