how to output the array result into a matrix


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to output the array result into a matrix
# 1  
Old 10-07-2012
how to output the array result into a matrix

I have a file like this:

HTML Code:
ASSPASVFETQY,hTRBV12-4,hTRBJ2-5,2
ASSPASTGGDYGYT,hTRBV18,hTRBJ1-2,2
ASSPASGDGYT,hTRBV5-1,hTRBJ1-2,2
ASSPASFPEDTQY,hTRBV27,hTRBJ2-3,2
ASSPARVNYGYT,hTRBV5-1,hTRBJ1-2,2
ASSPARTSGGLNEQF,hTRBV6-4,hTRBJ2-1,2
ASSPARQSYNEQF,hTRBV11-1,hTRBJ2-1,2
ASSPARQGARVDQPQH,hTRBV18,hTRBJ1-5,2
ASSPAQPRTNEKLF,hTRBV18,hTRBJ1-4,2
ASSPAQGYHTEAF,hTRBV18,hTRBJ1-1,2
I use the following code to count the frequency of each pair($2 VS $3)

Code:
awk '{ A[$2" "$3]+=$4 } END { for(X in A) print X, A[X] }' filename

The output file show result line by line. How can I get the result is matrix based on $2 and $3?

Thank you!
# 2  
Old 10-07-2012
Don't forget to set the input field separator: awk -F, ..
# 3  
Old 10-07-2012
If I have interpreted your request correctly and you're looking for output like:
Code:
          hTRBJ2-5 hTRBJ1-2 hTRBJ2-3 hTRBJ2-1 hTRBJ1-5 hTRBJ1-4 hTRBJ1-1
--------- -------- -------- -------- -------- -------- -------- --------
hTRBV12-4        2        0        0        0        0        0        0
  hTRBV18        0        2        0        0        2        2        2
 hTRBV5-1        0        4        0        0        0        0        0
  hTRBV27        0        0        2        0        0        0        0
 hTRBV6-4        0        0        0        2        0        0        0
hTRBV11-1        0        0        0        2        0        0        0

The following produces the above when a file named filename contains the input you provided in the 1st message in this thread:
Code:
#!/bin/ksh
awk -F, '{
        A[$2" "$3] += $4
        if(! ($2 in row)) {
                # We haev a new $2 value which will be printed as a new row.
                # Save ordering info for this row and max width of column 1.
                row[$2] = ++rc
                rh[rc] = $2
                if(cw[0] < length($2)) cw[0] = length($2)
        }
        if(! ($3 in col)) {
                # We have a new $3 value which will be printed as a new column.
                # Save ordering info and width for this column
                col[$3] = ++cc
                ch[cc] = $3
                cw[cc] = length($3)
        }
}
END {   # Print header rows
        for(i = 0; i <= cc; i++)
                printf("%*.*s%s", cw[i], cw[i], ch[i], i < cc ? " " : "\n")
        for(i = 0; i <= cc; i++)
                printf("%*.*s%s",
                        cw[i], cw[i], "------------------", i < cc ? " " : "\n")
        # Print data rows
        for(i = 1; i <= rc; i++) {
                # Print 1st two columns.
                printf("%*s%*d", cw[0], rh[i], cw[1] + 1, A[rh[i] " " ch[1]])
                # Print remaining columns.
                for(j = 2; j <= cc; j++)
                        printf("%*d", cw[j] + 1, A[rh[i] " " ch[j]])
                printf("\n")
        }
}' filename

This User Gave Thanks to Don Cragun For This Post:
# 4  
Old 10-07-2012
Thanks for your kindly help!

Last edited by xshang; 10-16-2012 at 04:55 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help w/ Reading Matrix & Storing in dynamic array

First of I would just like to state that I am not looking for you guys to just do my work for me, I do want to learn and actually understand everything that is happening. Hey all, I am having trouble on this. What I need to do is... Write an executable C file that will take a text file (not a... (8 Replies)
Discussion started by: innvert
8 Replies

2. Shell Programming and Scripting

ksh : Building an array based on condition result

I want to build an Errorlog. I would like to build an array as I move through the if statements and print the array once all error conditions have been defined. The results need to be comma delimited. tsver will be static "1.9.6(2)" other vars $prit $lt $rt can have the same or a different... (1 Reply)
Discussion started by: popeye
1 Replies

3. Shell Programming and Scripting

Assign zero to strings that don't appear in block, store result in AWK array

Hi to all, I have this input: <group> <x "2">Group D</x> <x "3">Group B</x> <x "1">Group A</x> </group> <group> <x "1">Group E</x> <x "0">Group B</x> <x "1">Group C</x> </group> <group> ... (11 Replies)
Discussion started by: Ophiuchus
11 Replies

4. Shell Programming and Scripting

How to put db2 query result into an array in shell script?

Hello, Can someone please advise me how to put the db2 query reult into an array? For example, the query reults are: string A string B string C Then how do I put them into array=string A array=string B ... (2 Replies)
Discussion started by: hanul
2 Replies

5. Shell Programming and Scripting

Result set into an array

Hi, I have an issue with the result set. I wanted to run db2 query against db2 server in unix environment using perl script. I wanted to get the result set into an array. $db=<<DB_Name>> connect to $db get connection state this is my query = SELECT DISTINCT 'R' FROM... (0 Replies)
Discussion started by: solo123
0 Replies

6. Shell Programming and Scripting

BASH: print matrix from single array

I am creating a report in groff and need to format data from a file into a table cell. Sample data: dador,173323,bpt,jsp,39030013338878,1 dador,173323,brew,jsp,39030013338860,1 dador,173323,brew,jsp,39030013339447,1 dador,173323,brew,jsp,39030013339538,1 I would like to build a table... (12 Replies)
Discussion started by: Bubnoff
12 Replies

7. Shell Programming and Scripting

Prase a file and store and result to an array

Dear all, I have a file having the following formats: ThreadFail=Web1=1234 ThreadFail=Web2=2345 ThreadFail=Web3=12 ConnectionFail=DB1=11 ConnectionFail=DB2=22 The number of lines will be different from every time . How can I parse the file and store the result to an a array inside... (6 Replies)
Discussion started by: youareapkman
6 Replies

8. Shell Programming and Scripting

Prase a file and stored the result to an array

Dear all , I have a file whose content has the following format: jboss.web:type=ThreadPool,name=AAAA jboss.web:type=ThreadPool,name=BBBB How can I parse this file to get the value of the name of each line (AAAA , BBBB) and store the result to an array ? (array = AAAA , array = BBBB).... (4 Replies)
Discussion started by: youareapkman
4 Replies

9. Shell Programming and Scripting

How to store query multiple result in shell script variable(Array)

:) Suppose,I have one table A. Table A have one column. Table A have 10 rows. I want this 10 rows store into shell script variable. like #!/bin/ksh v_shell_var=Hi here in call oracle , through loop How can I store table A's 10 rows into v_shell_var (Shell Script Array). Regards, Div (4 Replies)
Discussion started by: div_Neev
4 Replies

10. Shell Programming and Scripting

output result (period -1)

hello, I have the following basic script which input the period, but I would like to output is period -1.. how? echo Please input period (yyyymm): read PERIOD OUTPUT=$PERIOD -1 echo " you request period -1 is $OUT" input 200705 output 200704 input 200701 output 200612 The above... (4 Replies)
Discussion started by: happyv
4 Replies
Login or Register to Ask a Question