BASH: print matrix from single array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting BASH: print matrix from single array
# 8  
Old 04-30-2010
Quote:
Originally Posted by Bubnoff
Am I accessing the next variable with:
Code:
next=${ARRAY2[((cols*((row-1))+col))]}

...?
Yes, but if bib is at the last column, next will be the first value of the next row.
So, you'll end up with
Code:
0, 1, 2, 3,
4, 5, 6, 7,
8, 9, 10, 11,
12, 13, 14, 15

(no comma after 15)
If you want to remove the last comma on every row, I suggest you to test:
Code:
if ((col=cols)); 
instead of
if [[ -z "$next" ];

Hope this helps Smilie
# 9  
Old 04-30-2010
Quote:
Originally Posted by vgersh99
not really really sure of your description and sample files, but....

nawk -f bub.awk sampleData.txt

bub.awk:
Code:
BEGIN {
  FS=","
}
{
  c2[$2]=(c2[$2])?c2[$2] FS $1:$1
}
END {
  for (i in c2)
    printf("<tr>\n<td>%s</td><td>%s</td>\n</tr>\n", i, c2[i])
}

Actually, I've completely misunderstood it - never mind me....
This is very close! This is what I used:
Code:
BEGIN {
  FS=","
}
{
  c2[$2]=(c2[$2])?c2[$2] FS $3:$3
}
END {
  for (i in c2) 
    printf("<tr>\n<td>%s</td><td>%s</td>\n</tr>\n", i, c2[i])
}

And I get ( row sample ):
Code:
<td>173323</td><td>bpt,brew,brew,brew,eph,eph,eph,eph,
eph,ew,ew,ew,ew,ew,ew,ew,ew,ew,ew,ew,ew,ew,ew,ew,ew,
mat,mat,mat,mat,ml,ml,ml,ml,ml,ml,ml,ml,ml,ml,ml,ml,ml,ml,ml,ml,
ml,ml,ml,ml,ml,mol,mol,mol,mol,mol,mol,mol,mol,ncrl,ncrl,ncrl,ncrl,
ncrl,ncrl,ncrl,ncrl,ncrl,ncrl,okan,okan,okan,okan,pl,wpl,wpl</td>
</tr>

The other parameters are that the second column must be "sort | uniq"ed
and have a new line after every fifth sub column.

Like this:
Code:
<td>173323</td><td>
bpt,brew,eph,ew,mat,<newline>
mat,ml,mol,ncrl,okan,<newline>
pl,wpl,wpl</td>
</tr>





---------- Post updated at 02:48 PM ---------- Previous update was at 02:42 PM ----------

@tukuyomi ~

Thanks for the suggestion, either way works but your suggestion looks a
bit cleaner.

I'm trying to keep the groff markup as readable as possible in the tables
I'm auto-generating. Restricting the columns will help immensely.

Bub

Last edited by Bubnoff; 04-30-2010 at 06:42 PM.. Reason: Typo
# 10  
Old 04-30-2010
bub.awk:
Code:
BEGIN {
  FS=OFS=","
  nl=5

}
{
  if (!c2[$2])
    c2[$2]=$1
  else {
    n=split(c2[$2], a, OFS)
    for(i=1;i<=n;i++)
       if (a[i] == $1) break

    if (i>n)
       c2[$2]=c2[$2] OFS (!(n%nl)?ORS:"") $1
  }
}
END {
  for (i in c2)
    printf("<tr>\n<td>%s</td><td>\n%s</td>\n</tr>\n", i, c2[i])
}


Last edited by vgersh99; 04-30-2010 at 07:01 PM.. Reason: better formating
# 11  
Old 04-30-2010
@vgersh99

Thanks, that works!

Is it possible to add other arrays in the same statement, using the unique
field like before?

Sample output ( headings added to clarify objective ):
Code:
<th>bib#</th>
<th>locations</th>
<th>Collections</th>
<th>iType</th>
<tr>
<td>173323</td><td>
bpt,brew,eph,ew,mat,
ml,ml,mol,ncrl,okan,
omak,omak,pesh,rc,wpl</td><td>nj, nb, mjcd</td>b<td>
</tr>
<tr>

Column one is the key and the rest are collected values that match
the key.

Thanks for your time -- I think I've got enough to work with, to finish the
script with the responses already ...but if you're itching for a challenge. Smilie

I will probably use multiple statements to do this.

Bub
# 12  
Old 04-30-2010
what was the input for this sample output?
need to visualize this......
# 13  
Old 04-30-2010
vgersh99 ~

The input file looks like:
Code:
dador,173323,bpt,jsp,39030013338878,1
dador,173323,brew,jsp,39030013338860,b
dador,173323,brew,jsp,39030013339447,b
dador,173323,brew,jsp,39030013339538,b
dador,173323,eph,jsp,39030008419089,ac
dador,173323,eph,jsp,39030013338902,ac
dador,173323,eph,jsp,39030013339413,b
dador,173323,eph,jsp,39030013339561,pl
dador,173323,eph,jsp,39030013340122,b
dador,173323,ew,jsp,39030013338928,ac

The first column is the title, second is bib#, third is location, forth is
barcode ( not used in table ) and last is itype.

The table would consist of rows organized by the bib# which would be presented once ( unique ). Its associated title would be in the second column, which is not an issue because the bib# can only have one possible title. The tricky part is with the location, itype and collection columns as they have numerous possibilities and must all be listed in their respective columns next to the bib#. So the table is five columns and a sample row would look like this ( sorry for the ascii table ...but html doesn't work here ):

Code:
| bib# | title   | locations             | itypes| collections
---------------------------------------------------------------
173323 | dador   | brew, ml, wat, eph, ew| b, ac | mjcd, nj, j, jsp 
       |         | cc, bkm, mat, ent, oro|       |         
---------------------------------------------------------------

So the challenge is to collect the info in the last three columns through
a sort \ uniq, then print with the third restricted to 5 wide.

Hope this makes sense.

Bub

---------- Post updated at 05:59 PM ---------- Previous update was at 03:59 PM ----------

Here's where I'm at. The awk script is cool but I can't figure out how to expand it to meet the parameters of the 5 column table ( my fault for not being clear about the objectives at the beginning ). This uses the bash loop
suggested by tukuyomi + some awk. I will use a similar technique to add the last two columns.

If anyone has a more terse or efficient solution, I'll be watching.
Code:
#!/bin/bash
FILE=04-28-2010.clean

# bibs
A=($(awk -F"," '{print $2}' $FILE | sort | uniq))
ELEMENTS=${#A[@]}
cols=5
rows=$((ELEMENTS/cols+1))

for (( i=0;i<$ELEMENTS;i++)); do
        # bib
        bib=${A[${i}]}
        printf "$bib\t"
        # titles
        T=$(awk -F"," "\$2 ~ /$bib/ {print \$1}" $FILE | uniq)
        printf "$T\tT{\n"
        # location
        L=($(awk -F"," "\$2 ~ /$bib/ {print \$3}" $FILE | sort | uniq))
        for (( row=1; row<=rows; row++)); do
            for ((col=1; col<=cols; col++)); do
                loc=${L[((cols*((row-1))+col-1))]}
                next=${L[((cols*((row-1))+col))]}
                if [[ -z "$next" ]]
                then
                    printf "$loc"
                else
                    printf "%s, " "$loc"
                fi
             done
         done
         printf "\nT}\n"
done


Here's the output ( it's for a table row in groff ):

Code:
136671  giver   T{
man, ml, pat, rep, wpl
T}m, bpt, brew, bsmt, cash, cc, che, club, ent, eph, 
173323  dador   T{
bpt, brew, eph, ew, mat, ml, mol, ncrl, okan, omak, 
T}sh, eph, ew, gc, ml, mol, rc, rep, twi, wpl
263941  giver   T{
club38  giver   T{
T}l
284472  giver   T{
bkm, bpt, brew, bsmt, cash, cc, che, club, ent, eph, 
T}
360549  giver   T{
cash, eph, ew, gc, ml, mol, rc, rep, twi, wpl
T}
368038  giver   T{
mol
T}

Still not quite there ...but close.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to print median values of matrix -awk?

I use the following script to print the sum and how could I extend this to print medians instead? thanks name s1 s2 s3 s4 g1 2 8 6 5 g1 5 7 9 9 g1 6 7 8 9 g2 8 8 8 8 g2 7 7 7 7 g2 10 10 10 10 g3 3 12 1 24 g3 5 5 24 48 g3 12 3 12 12 g3 2 3 3 3 output name s1 s2 s3 s4 g1 5 7 8 9... (5 Replies)
Discussion started by: quincyjones
5 Replies

2. UNIX for Beginners Questions & Answers

Limiting Bash array single line output

#!/bin/bash PH=(AD QD QC 5H 6C 8C 7D JH 3H 3S) echo ${PH} In the above array, how can I print to screen just the first 8 elements of ${PH} and have the last 2 elements print just below the first line starting underneath AD? I need to do this in order to save terminal window spacing... (5 Replies)
Discussion started by: cogiz
5 Replies

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

4. Shell Programming and Scripting

Make Separated files from a single matrix - Perl

Hey Masters, Here is my input: fragmentID chromosome start end HEL25E TRIP1 r5GATC2L00037 chr2L 5301 6026 0.03 0.036 r5GATC2L00038 chr2L 6023 6882 -0.025 -0.041 r5GATC2L00040 chr2R 6921 7695 -0.031 0.005 r5GATC2L00042 chr2R 7715 8554 -0.006 -0.024 r5GATC2L00043 chr3L 8551 8798 0.042 0... (4 Replies)
Discussion started by: @man
4 Replies

5. Shell Programming and Scripting

Print array into a single file - AWK

Hi all, I been looking for a solution to the fact that when I use: for (i=1; i<=NF; i++) print $ifields that are originally in a single line are printed in a single line I have severals files for which the first 7 are the same, but the number of variables after that can vary, for example NF... (5 Replies)
Discussion started by: PaulaL
5 Replies

6. Shell Programming and Scripting

how to output the array result into a matrix

I have a file like this: 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... (3 Replies)
Discussion started by: xshang
3 Replies

7. UNIX for Dummies Questions & Answers

BASH - Creating a Matrix

I'm trying to create a Matrix using bash. The expected output is .AB CDE FG 1 2 3 4 5 6 7 I'm a newbie in shell language, really appreciate if there is anyone who can guide me with this. Double post again, continued here (0 Replies)
Discussion started by: vinzping
0 Replies

8. Shell Programming and Scripting

Perl question - How do I print contents of an array on a single line?

I have the following code: print @testarray; which returns: 8 8 8 9 How do I return the array like this: The output is: 8, 8, 8, 9 (5 Replies)
Discussion started by: streetfighter2
5 Replies

9. Shell Programming and Scripting

How can I print array elements as different columns in bash?

Hi Guys, I have an array which has numbers including blanks as follows: 1 26 66 4.77 -0.58 88 99 11 12 333 I want to print a group of three elements as a different column in a file as follows:(including blanks where there is missing elements) for.e.g. array element #7... (4 Replies)
Discussion started by: npatwardhan
4 Replies

10. Shell Programming and Scripting

PERL: How do i print an associative matrix?

Hello guys, I have in PERL an associative 2-dimensional array, called matrix. The array (actually the matrix) is made up like this matrix = x; matrix = y; matrix = w; matrix = z; ... but the names a, b, c, d are set just at runtime. The question is: how can i get all the keys of... (2 Replies)
Discussion started by: foo.bar
2 Replies
Login or Register to Ask a Question