awk small discussion on array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk small discussion on array
# 1  
Old 09-16-2013
awk small discussion on array

Hi...all

I want to pass array name for some function using loop anyone is having idea ?

here is scenario

Code:
echo | awk ' 
        BEGIN{
            A1[1]="foo1"
            A2[1]="foo2"
            A3[1]="foo3"
             }

        function test1(a,b,c){
                    print a,b,c
                     }
        function test2(a,b,c){
                    print b[1]
                     }
        {
        for(i=1;i<=4;i++)
                test1(i,"A"i,"B"i+1)
                test2(i,"A"i,"B"i+1)
        }'

Resulting
Code:
sh test.sh 
1 A1 B2
2 A2 B3
3 A3 B4
4 A4 B5
awk: cmd. line:11: (FILENAME=- FNR=1) fatal: attempt to use scalar parameter `b' as an array

someone help me..I don't know whether syntax is legal or not...waiting for quick solution....from unix experts...

Last edited by Akshay Hegde; 09-16-2013 at 09:03 AM..
# 2  
Old 09-16-2013
You are not passing any array, just a string is being passed and thats why the error.

Try like this:

Code:
 

                test1(i,"A"i,"B"i+1);
                split("hi:aa:ab",c,":");
                test2(i,c,"B"i+1);

Guru.
# 3  
Old 09-16-2013
Thanks for reply....

in
Code:
BIGIN

block I have defined array
Code:
A1[1],A2[1]..

here I want to send array name to function.In first function test1 I am just printing only arguments thats is
Code:
print a,b,c

what you said is
Code:
array c contains index 1....n , that is c[1],c[2]...c[n]

but in my case I have
Code:
array c1[1]..c2[1]...c5[1]....cn[1]

I think something going wrong while calling function test2..here my interest is to use loop index along with string A to call function. as you know calling array n number of times manually is laborious like below

Code:
test2(i,A1,"B"i+1)
test2(i,A2,"B"i+1)
test2(i,A3,"B"i+1)
test2(i,A4,"B"i+1)


Last edited by Akshay Hegde; 09-16-2013 at 11:29 AM..
# 4  
Old 09-16-2013
This is something which can be done with evalin bash I think.
What is your original requirement anyways? May be we can sort that out somehow.

--ahamed
# 5  
Old 09-16-2013
You mean it's impossible in awk ?? what I posted is my requirement..I am loosing my hopes...

Last edited by Akshay Hegde; 09-16-2013 at 10:38 AM.. Reason: typo
# 6  
Old 09-16-2013
You have not posted your requirement, only the manner you are determined to solve said requirement... This is a very common failure mode where people get trapped working on the very first solution which comes to mind to the point they cannot imagine other solutions.

If you could post your actual goal, there may be a much simpler way.
These 3 Users Gave Thanks to Corona688 For This Post:
# 7  
Old 09-16-2013
Thanks Corona688 for reply. I am just playing with array.. first I tried like this
Code:
echo | awk ' 
        BEGIN{
            A[0,1]="foo1"
            A[0,2]="foo2"
            A[0,3]="foo3"
             }

        function test2(a,b,c){
                    print b[0,a]
                     }
        {
        for(i=1;i<=4;i++)
                test2(i,A,"B"i+1)
        }'

got result like this as expected
Code:
sh test.sh 
foo1
foo2
foo3

then I tried in another way which I posted, please note which I posted is not classroom homework, I just wanted to know,why I am getting fatal: attempt to use scalar parameter `b' as an array, I was keep on searching solution on google, I couldn't find it, so I posted. I request moderator please close this post if there is no solution.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

HELP - NEED Small Script ( or single awk Command)

Hi All REQUIREMENT:- ========== There is one folder named /data/ds/dpr_ukdw_sys/working/unixfile In that folders there are files are like below CODS_ACTMZ_TRANS_ALL_20150911.TXT CODS_ACTMZ_REF_CTR_ACT_MIL_20150911.TXT CODS_ACTMZ_REF_NHA_ALL_20150911.TXT ... (4 Replies)
Discussion started by: Nagarjuna4347
4 Replies

2. Shell Programming and Scripting

How to Assign an shell array to awk array?

Hello All, Can you please help me with the below. #!/bin/bash ARR="No Differences In Stage Between HASH_TOTALS & HASH_TOTALS_COMP For UNINUM:0722075 PROVIDER:5 EXTRACT_DT:30-SEP-12 VER_NUM:1" ARR="No Differences In Stage Between HASH_TOTALS & HASH_TOTALS_COMP For UNINUM:0722075 PROVIDER:5... (14 Replies)
Discussion started by: Ariean
14 Replies

3. Shell Programming and Scripting

Split a large array into small chunks

Hi, I need to split a large array "@sharedArray" into 10 small arrays. The arrays should be like @sharedArray1,@sharedArray2,@sharedArray3...so on.. Can anyone help me with the logic to do so :(:confused: (6 Replies)
Discussion started by: rkrish
6 Replies

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

5. UNIX for Dummies Questions & Answers

AWK - changing first char from small to upper

I need to write script in AWK, changing first char from a line from lower to upper. I found function toupper etc. but have no idea how to sent only first char from every line instead of the whole line. Anyone has any idea? // Sorry for my english:D (8 Replies)
Discussion started by: bbqtoss
8 Replies

6. UNIX for Dummies Questions & Answers

small doubt in awk

Hi Guys, I have a small problem with awk. I want to search { and } at a particular position in a string using awk. I tried echo "hello{hi" | awk '{if(substr($0,6,1)=="{"){print "TRUE"}}' but no success :mad: i know it can be done in many ways and i know them also.. but my... (6 Replies)
Discussion started by: vidyadhar85
6 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. UNIX for Dummies Questions & Answers

A small AWK problem

I have a file tmp.out with contents: 2008-08-09 05:11:01 2008-08-09 08:52:59 2008-08-11 12:08:34 2008-08-11 12:15:40 I want the output to be: 3|0|1|71|2008-08-09 05:11:01|2008-08-30 11:19:28 4|0|1|71|2008-08-09 08:52:59|2008-08-30 11:19:28 5|0|1|71|2008-08-11 12:08:34|2008-08-30 11:19:28... (6 Replies)
Discussion started by: ChicagoBlues
6 Replies

9. Shell Programming and Scripting

Please help to debug a small shell script (maybe AWK problem)?

Hi Buddies, The following is shell scripts which was borrowed from linux box for load average check. it runs good. (this structure is simple, when load average is too high, it will send alert to user) #!/usr/bin/ksh # Set threshold for 1, 5 and 15 minture load avarage # configured for... (4 Replies)
Discussion started by: GreatJerry
4 Replies

10. Shell Programming and Scripting

AWK deifnition and some small code explanation

Could someone give me a quick simple explanation for the AWK command. And also help me to explain the code i have made. I have made some general comments about it myself. I was wondering if people could help me with the rest: awk -F'' 'END { fmt = "%-20s\t%s\t%s\n" ... (0 Replies)
Discussion started by: amatuer_lee_3
0 Replies
Login or Register to Ask a Question