How order a data matrix using awk?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How order a data matrix using awk?
# 1  
Old 02-21-2014
How order a data matrix using awk?

is it possible to order the following row clusters from ascending to descending. thanx in advance


input

Code:
1       2       4       0
1       2       4       0
3       3       3       3
1       5       1       0
1       5       1       0
6       0       0      0
5       1      1       1
1       6       1      1

output
Code:
6       0       0      0
5       1      1       1
1       6       1      1
1       5       1       0
1       5       1       0
1       2       4       0
1       2       4       0
3       3       3       3

# 2  
Old 02-21-2014
What have you tried?
# 3  
Old 02-21-2014
I did some thing like this (at least to get the top clusters that I wanted).

Code:
awk '{if ($1> $2&&$3&&$4) print $0}'
awk '{if ($2> $1&&$3&&$4) print $0}'
awk '{if ($3> $2&&$1&&$4) print $0}'
awk '{if ($4> $2&&$3&&$1) print $0}'

# 4  
Old 02-21-2014
Hi,

Could be easily done using sort.

Code:
sort -nr

Sould be fine.
# 5  
Old 02-21-2014
Your code in #3 and you output specification seems to suggest you first would like to print the line that have the last maximum in the first column, then the second column, etc..
For starters you could try something like this, where the output is grouped but not sorted.
Code:
awk '
  {
    m=1
    for(i=1; i<=NF; i++) if($m<=$i)m=i
    A[m]=A[m] $0 ORS
  } 
  END{
    for(i=1; i<=4; i++) printf "%s",A[i]
  }
' file

Output:
Code:
6       0       0      0
5       1      1       1
1       5       1       0
1       5       1       0
1       6       1      1
1       2       4       0
1       2       4       0
3       3       3       3

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[Solved] Converting the data into matrix with 0's and 1's

I have a file that contains 2 columns tag,pos cat input_file tag pos atg 10 ata 16 agt 15 agg 19 atg 17 agg 14 I have used following command to sort the file based on second column sort -k 2 input_file tag pos atg 10 agg 14 agt 15 ata 16 agg 19 atg 17 (2 Replies)
Discussion started by: raj_k
2 Replies

2. Shell Programming and Scripting

How to insert gaussian noise to a data with an order with awk?

Hello everybody, My problem is inserting gaussian noise to a file containing data, that is, add every single noise to the number after every 6 character. here is my data 0910 1 2048 32.38 40 20.88 28 35.53 9.62 0.00 MSDMP0 1.790MSDMS1 2.840KCTXP2 4.400KRCMP0 4.600BOZMP0 5.640BOZMS2... (26 Replies)
Discussion started by: miriammiriam
26 Replies

3. Shell Programming and Scripting

Reformatting data in matrix form

Hi, Some assistance with respect to the following problem will be very helpful. I want to reformat my dataset in the following manner for subsequent analysis. I have first column values (which repeat for each value of 2nd column) which are names, the second column specifies position ad the... (1 Reply)
Discussion started by: newbie83
1 Replies

4. Shell Programming and Scripting

convert data into matrix- awk

is it possible to count the number of keys based on state and cell and output it as a simple matrix. Ex: cell1-state1 has 2 keys cell3-state1 has 4 keys. Note: Insert 0 if no data available. input key states cell key1 state1 cell1 key1 state2 cell1 key1 ... (21 Replies)
Discussion started by: quincyjones
21 Replies

5. Ubuntu

How to convert full data matrix to linearised left data matrix?

Hi all, Is there a way to convert full data matrix to linearised left data matrix? e.g full data matrix Bh1 Bh2 Bh3 Bh4 Bh5 Bh6 Bh7 Bh1 0 0.241058 0.236129 0.244397 0.237479 0.240767 0.245245 Bh2 0.241058 0 0.240594 0.241931 0.241975 ... (8 Replies)
Discussion started by: evoll
8 Replies

6. Shell Programming and Scripting

two-column data to matrix in AWK

Howdy, I need to convert an association data matrix, currently in a two-column format, into a matrix with numbers indicating the number of associations. I've been looking around for AWK code in the list, but could not find anything. Here's an example of what I want to perform: original... (10 Replies)
Discussion started by: sramirez
10 Replies

7. Shell Programming and Scripting

Invert Matrix of Data - Perl

I have columnar data in arrays perl, Example - @a = (1,2,3); @array1 = (A,B,C); @array2 = (D,E,F); @array3 = (I,R,T); I want the data to be formatted and printed as 1 A D I 2 B E F 3 C F T and so on... (8 Replies)
Discussion started by: dinjo_jo
8 Replies

8. Shell Programming and Scripting

extract data from a data matrix with filter criteria

Here is what old matrix look like, IDs X1 X2 Y1 Y2 10914061 -0.364613333 -0.362922333 0.001691 -0.450094667 10855062 0.845956333 0.860396667 0.014440333 1.483899333... (7 Replies)
Discussion started by: ssshen
7 Replies

9. UNIX for Dummies Questions & Answers

changing data into matrix form

Hi, I have a file whose structure is like this 7 7 1 2 3 4 5 1 3 4 8 6 1 4 5 6 0 2 6 8 3 8 2 5 7 8 0 5 7 9 4 1 3 8 0 2 2 3 5 6 8 basically first two row tell the number of rows and column but the data following them are not arranged in that format. now i want to create another... (1 Reply)
Discussion started by: g0600014
1 Replies

10. Shell Programming and Scripting

AWK - printing certain fields when field order changes in data file

I'm hoping someone can help me on this. I have a data file that greatly simplified might look like this: sec;src;dst;proto 421;10.10.10.1;10.10.10.2;tcp 426;10.10.10.3;10.10.10.4;udp 442;10.10.10.5;10.10.10.6;tcp sec;src;fac;dst;proto 521;10.10.10.1;ab;10.10.10.2;tcp... (3 Replies)
Discussion started by: eric4
3 Replies
Login or Register to Ask a Question