awk multidimensional values


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk multidimensional values
# 1  
Old 11-05-2008
awk multidimensional values

Hello All,

Here is my input file content and i am trying to write an awk file that produces the output as shown below. i tried with my little knowledge but was not successful.

Input File content:

ID COL1 COL2 COL3
1002 1 val21 val31
1002 2 val22 val32
1002 3 val23 val33
1002 4 val24 val34
1111 1 abc21 abc31
1111 2 abc22 abc32
3124 1 xyz21 pbq31
3124 2 pbq pbq
3124 3 rad brs

Output expected is:
update/1002=(COL1=[1,2,3,4], COL2=[val21,val22,val23,val24], COL3=[val31,val32,val33,val34])
update/1111=(COL1=[1,2], COL2=[abc21,abc22], COL3=[ABC31,ABC32])
update/3124=(COL1=[1,2,3], COL2=[pbq,pbq], COL3=[rad,brs])

I am also attaching my input and output files in case the above content is not clear to read.

I can manage to get the output like this but this is not what i expect
update/1002=(COL1=[1], COL2=[val21], COL3=[val31])
update/1002=(COL1=[2], COL2=[val22], COL3=[val32])

and so on...

Can any one help me please.

Many thanks for your help in advance.

Last edited by forumthreads; 11-05-2008 at 11:17 AM..
# 2  
Old 11-06-2008
Try...
Code:
NR > 1  {
    if (a[$1] == $1) {
        b[$1] = b[$1] "," $2
        c[$1] = c[$1] "," $3
        d[$1] = d[$1] "," $4
    } else {
        a[$1] = $1
        b[$1] = $2
        c[$1] = $3
        d[$1] = $4
    }
}

END {
    for (i in a) {
            printf "update/%s={[%s], COL2=[%s], COL3=[%s])\n", a[i], b[i], c[i], d[i]
    }
}

# 3  
Old 11-06-2008
Many Thanks Ygor.

Will try and let you know the result.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Multidimensional array:awk error

awk -F'\t' -v OFS='\t' ' { if($2 in arr) { #print "Sahi", NR,arr for(k=2;k<=NF;k++){ # sum]+=$2 } } else { arr=NR #print "awk",NR for (k=3;k<=NF ; k++){ sum=$k } } } (7 Replies)
Discussion started by: genome
7 Replies

2. Shell Programming and Scripting

awk file to read values from Db2 table replacing hard coded values

Hi, I want to replace a chain of if-else statement in an old AWK file with values from Db2 table or CSV file. The part of code is below... if (start_new_rec=="true"){ exclude_user="false"; user=toupper($6); match(user, "XXXXX."); if (RSTART ==2 ) { ... (9 Replies)
Discussion started by: asandy1234
9 Replies

3. Shell Programming and Scripting

How to find the X highest values in a list depending on the values of another list with bash/awk?

Hi everyone, This is an exemple of inpout.txt file (a "," delimited text file which can be open as csv file): ID, Code, Value, Store SP|01, AABBCDE, 15, 3 SP|01, AABBCDE, 14, 2 SP|01, AABBCDF, 13, 2 SP|01, AABBCDE, 16, 3 SP|02, AABBCED, 15, 2 SP|01, AABBCDF, 12, 3 SP|01, AABBCDD,... (1 Reply)
Discussion started by: jeremy589
1 Replies

4. Shell Programming and Scripting

How to deal with multidimensional array in awk?

Hi all! I would like to know how to print $0 when using multidimensional array like below time being I am using for loop to print columns like this awk 'FNR==1{i++} {for(k=1;k<=NF;k++)A=$k} END{for(j=1;j<=25;j++) print A,A,A,A,A,A,A,A,A,A,A,A,A,A}' file1 file2 so here my problem is I... (5 Replies)
Discussion started by: Akshay Hegde
5 Replies

5. Shell Programming and Scripting

multidimensional array in awk

Hi, I was trying to process a file with the help of awk. I want to first display all the rows that contains 01 and at the end of processing I have to print some portion of all the lines. like below. Output expected: (2 Replies)
Discussion started by: ahmedwaseem2000
2 Replies

6. Shell Programming and Scripting

AWK: read values from file1; search for values in file2

I have read another post about this issue and am wondering how to adapt it to my own, much simpler, issue. I have a file of user IDs like so: 333333 321321 546465 ...etc I need to take each number and use it to print records wherein the 5th field matches the user ID pulled from the... (2 Replies)
Discussion started by: Bubnoff
2 Replies

7. Shell Programming and Scripting

multidimensional arrays using awk

i'm trying to use awk to count a listing similar to the following and get a report of the listing similar to the one below it. y,pizza n,pizza y,pizza y,pizza n,tomato n,tomato y,cheese y,cheese n,cheese report ---- pizza,3,1 tomato,0,2 cheese,2,1 (1 Reply)
Discussion started by: multimulti
1 Replies

8. Shell Programming and Scripting

How to pick values from column based on key values by usin AWK

Dear Guyz:) I have 2 different input files like this. I would like to pick the values or letters from the inputfile2 based on inputfile1 keys (A,F,N,X,Z). I have done similar task by using awk but in that case the inputfiles are similar like in inputfile2 (all keys in 1st column and values in... (16 Replies)
Discussion started by: repinementer
16 Replies

9. Shell Programming and Scripting

AWK multidimensional array

In a single dim. awk array, we can use : <index> in <array name> to determine whether a particualar index exists in the array or not. Is there a way to achieve this in a awk multi dim. array ? (4 Replies)
Discussion started by: sinpeak
4 Replies

10. Shell Programming and Scripting

Awk multidimensional Array

Hello Experts,, Can anybody give me a brief idea what is following bold letter statement is for!! what is the term called so that I can google for it.. It seems to be an array inside another array.. awk' /TXADDR/ { txaddr=$NF } ##understood /TXDATA/ { txdata]=$NF... (1 Reply)
Discussion started by: user_prady
1 Replies
Login or Register to Ask a Question