awk - array elements as condition


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk - array elements as condition
# 1  
Old 07-02-2009
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

Code:
if ( $1 ~ /^A[1]$|^A[2]$|^A[3]$/) { print  YES }

but i want something like..

Code:
if ( $1 ~ /^A[1]$|^A[2]$|^A[3]$/| logical OR till array's last element) { print  YES }


Is that possibe?
# 2  
Old 07-02-2009
When you say:
Code:
$1 ~ /^A[1]$/

That would be equivalent (if it worked) to:
Code:
$1 == A[1]

You can't use / ... / to do what you are trying to do.

Have a look at:
The GNU Awk User's Guide
(7.5 Scanning All Elements of an Array)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Get unique elements from Array

I have an array code and output is below: echo $1 while read -r fline; do echo "%%%%%%$fline%%%%%" fmy_array+=("$fline") done <<< "$1" Output: CR30903 YU0007 SRIL CR30903 Yogesh SRIL %%%%%%CR30903 YU0007 SRIL%%%%% %%%%%%CR30903 Yogesh SRIL%%%%% ... (8 Replies)
Discussion started by: mohtashims
8 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

Multiplication of array elements

Hi, I can't find out how to create correct code to get multiplication of each elements of array. Let's say I enter array into command line (2 3 4 5 6 8) and i need output 2*3*4*5*6*8=5760. I tried this one, but answer is 0. for i in $@; do mult=$((mult*i))done echo "mult: " $mult ... (4 Replies)
Discussion started by: rimasbimas
4 Replies

4. 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

5. Shell Programming and Scripting

HELP with AWK one-liner. Need to employ an If condition inside AWK to check for array variable ?

Hello experts, I'm stuck with this script for three days now. Here's what i need. I need to split a large delimited (,) file into 2 files based on the value present in the last field. Samp: Something.csv bca,adc,asdf,123,12C bca,adc,asdf,123,13C def,adc,asdf,123,12A I need this split... (6 Replies)
Discussion started by: shell_boy23
6 Replies

6. 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

7. 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

8. Shell Programming and Scripting

AWK help: how to compare array elements against a variable

i have an array call ignore. it is set up ignore=34th56 ignore=re45ty ignore=rt45yu . . ignore=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... (2 Replies)
Discussion started by: usustarr
2 Replies

9. Shell Programming and Scripting

Array with String Elements

How can I get my array to understand the double-quotes I'm passing into it are to separate text strings and not part of an element? here's what I'm working with... db2 -v connect to foo db2 -x "select '\"' || stats_command || '\",' from db2law1.parallel_runstats where tabname = 'BAZ'" set... (4 Replies)
Discussion started by: djschmitt
4 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
Login or Register to Ask a Question