AWK help: how to compare array elements against a variable


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
# 1  
AWK help: how to compare array elements against a variable

i have an array call ignore. it is set up
ignore[0]=34th56
ignore[1]=re45ty
ignore[2]=rt45yu
.
.
ignore[n]=rthg34

n is a variable. I have another variable that i read from a different file. It is $2 and it is working the way i expect. array ignore read and print correct values.

in the below if statement (2nd if statement where i use array ignore), i need to do following, if "$2" == 34th56 || "$2" == re45ty || .... "$2" == rt45yu .i dont want to do a while or for loop, because it is printing lots of junk that i dont want. Can any one help me with this please? Smilie



Code:
 
#! /usr/bin/ksh
# $2 is a variable and it is working the way i expect
# array ignore read and print correct values and it is working fine too
# in the if statement, i need to do following, if "$2" == ignore[0] || "$2" == ignore[1] || .... "$2" == ignore[n] 
#  { print "  " 
#   foundTermEvent = 0;
#   } 
#  else  {
#   print;
#   print " FOUND A BADTC  "  }

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

 if (foundTermEvent == 1)
       {
          if ( "$2" in ignore )  
          {
      
                print " DIDNT FIND BAD TC "
        #       { print $1 }
        #       { print $2 }
                foundTermEvent = 0;
       
          }
            else
            {
            print;
            print " FOUND A BADTC  "
        #    { print $1 }
           { print $2 }
            print " ########################## "
           }
}

# 2  
Ahem... you mentioned awk, but the script you provided seems to use ksh (line 1) while utilizing a mix of ksh- and awk-syntax. I'm not sure which environment you are actually using in your script.

If it is ksh: use "${arr[@]}", which puts the whole array into one string and use a grep of your input value to determine if it is part of it:

Code:
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

I hope this helps.

bakunin
# 3  
i am using AWK, and yes there is ksh too. but the part of the code i am working on is written in AWK.

i get a syntax error in below mention line. It doesnt seems to like red highlited brackets.
if [ $(print - "${ignore[@]}" | grep -c "$2") -gt 0 ] ; then
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #613
Difficulty: Medium
Examples of complex numbers in Python are (-5+4j) and (2.3 - 4.6j).
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compare multiple arrays elements using awk

I need your help to discover missing elements for each box. In theory each box should have 4 items: ITEM01, ITEM02, ITEM08, and ITEM10. Some boxes either have a missing item (BOX02 ITEM08) or might have da duplicate item (BOX03 ITEM02) and missing another one (BOX03 ITEM01). file01.txt ... (2 Replies)
Discussion started by: alex2005
2 Replies

2. Shell Programming and Scripting

Help reading the array and sum of the array elements

Hi All, need help with reading the array and sum of the array elements. given an array of integers of size N . You need to print the sum of the elements in the array, keeping in mind that some of those integers may be quite large. Input Format The first line of the input consists of an... (1 Reply)
Discussion started by: nishantrefound
1 Replies

3. Shell Programming and Scripting

Awk: Append new elements to an array

Hi all, I'm dealing with a bash script to merge the elements of a set of files and counting how many times each element is present. The last field is the file name. Sample files: head -5 *.tab==> 3J373_P15Ac1y2_01_LS.tab <== chr1 1956362 1956362 G A hom ... (7 Replies)
Discussion started by: lsantome
7 Replies

4. Shell Programming and Scripting

awk, associative array, compare files

i have a file like this < '393200103052';'H3G';'20081204' < '393200103059';'TIM';'20110111' < '393200103061';'TIM';'20060206' < '393200103064';'OPI';'20110623' > '393200103052';'HKG';'20081204' > '393200103056';'TIM';'20110111' > '393200103088';'TIM';'20060206' Now i have to generate a file... (9 Replies)
Discussion started by: shruthi123
9 Replies

5. Shell Programming and Scripting

Match elements in an AWK multi-dimensional array

Hello, I have two files in the following format; file1: A B C D E F G H I J K L file2: 1 2 3 4 5 6 7 8 9 10 11 12 I have read them both in to multi-dimensional arrays. I need a file that has column 2 of the first file printed out for each column 3 of the second file ie... ... (3 Replies)
Discussion started by: cold_Que
3 Replies

6. Shell Programming and Scripting

printing array elements inside AWK

i just want to dump my array and see if it contains the values i am expecting. It should print as follows, ignore=345fht ignore=rthfg56 . . . ignore=49568g Here is the code. Is this even possible to do? please help termReport.pl < $4 | dos2ux | head -2000 | awk ' BEGIN... (0 Replies)
Discussion started by: usustarr
0 Replies

7. Shell Programming and Scripting

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

Hi all, i have a data array as follows. array=ertfgj2345 array=456ttygkd . . . array=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). ... (9 Replies)
Discussion started by: usustarr
9 Replies

8. Shell Programming and Scripting

awk - array elements as condition

Hi, can I use array elements ( all ) in conditional statements? the problem is ,the total number of elements is not known. e.g A is an array with elements - 1,2,3 now if i want to test if the 1 st field of input record is either 1,2 or 3, i can do something like this if ( $1 ~... (1 Reply)
Discussion started by: shellwell
1 Replies

9. Shell Programming and Scripting

compare a variable against array elements

hi everyone - i have a bash script that does, in broad terms, the following: given a file containing a list of email accounts, for every account, do , then move on to the next account. pretty simple, and all that stuff works fine. thing is, there's a very good change that any account... (2 Replies)
Discussion started by: fearboy
2 Replies

10. Shell Programming and Scripting

Accessing single elements of a awk array in END

How do I access one of the indices in array tst with the code below? tst=sprintf("%5.2f",Car / 12) When I scan thru the array with for ( i in tst ) { print i,tst } I get the output of: vec-7 144 But when I try this in the END print tst It looks like it's not set. What am... (6 Replies)
Discussion started by: timj123
6 Replies

Featured Tech Videos