How to sort matrix table in UNIX?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to sort matrix table in UNIX?
# 1  
Old 03-01-2013
How to sort matrix table in UNIX?

Hello All,
i have a file sort.txt with below entries.
1 12 10
16 6 4
20 8 15

i need to sort these entries and the out put should come in a single line.

1 4 6 8 10 12 15 16 20

Can you please help me sort this out?
# 2  
Old 03-01-2013
Would you consider:-
Code:
cat sort.txt | tr " " " ^J" |sort -n | while read line
do
   print "$line \c"
done;print

Not pretty, but it seems to work. The ^J bit is a single character, usually generated by the key sequence <CNTRL>v then ENTER.

What this does is to break each line up to separate lines using a space as a delimiter. It then sorts then numerically and then connects them all back together on a single output line using the \c to avoid splitting the line and finishing with an extra print command to ensure you get the output complete with a new-line.


There may well be prettier or more efficient ways to do this. How big is your file?

I hope that this helps.
Robin
Liverpool/Blackburn
UK
# 3  
Old 03-01-2013
An approach using gawk and asort sorting function:
Code:
gawk ' {
        for(i=1; i<=NF; i++)
                a[++j] = $i
} END {
        n = asort(a)
        for(i=1; i<=n; i++)
                printf "%d ", a[i]
} ' sort.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

UNIX shell script for matrix insertion into a file

I have a script which is extracting data from a database in an excel file in below given format, date_time calling_app1 count of requests date calling_app x 34 date calling_app y 1034 I want to write a script which will write data into a file like this ... (3 Replies)
Discussion started by: TNT47
3 Replies

2. Shell Programming and Scripting

awk to convert table-by-row to matrix table

Hello, I need some help to reformat this table-by-row to matrix? infile: site1 A:o,p,q,r,s,t site1 C:y,u site1 T:v,w site1 -:x,z site2 A:p,r,t,v,w,z site2 C:u,y site2 G:q,s site2 -:o,x site3 A:o,q,s,t,u,z site3 C:y site3 T:v,w,x site3 -:p,routfile: SITE o p q r s t v u w x y... (7 Replies)
Discussion started by: yifangt
7 Replies

3. Shell Programming and Scripting

Read in Table as a matrix

Dear forum users, i'm trying to read a table with 40x122 data in a array. Following this, i'd plot each rows again the header of the file in gnuplot. i was thinking for something like that #!/bin/bash # reads from the $ips file and assigns to $MYARRAY #IFS =";" split the line after the... (6 Replies)
Discussion started by: servuskelb
6 Replies

4. Shell Programming and Scripting

Table to Matrix

Hi, I have a table in the format: 1 0 -1 1 0 2 0 1 -1 0 0 0 3 0 1 1 0 0 0 0 0 0 etc. I am trying to input this to a program, however it is complaining about the fact that it is not in matrix format. How do I add 0's to end of the rows to make them even? Thanks in advance! (2 Replies)
Discussion started by: Rhavin
2 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

Creating table in Unix

Hi All, In a given directory, I need to list the files present in it in the below given format as a table. File name Permission Number of Bytes File Type Telecom1 --w-r-x 1230 Directory Telecom2 ---x---x---x 450 Device file Telecom3 ... (7 Replies)
Discussion started by: mr_manii
7 Replies

7. Shell Programming and Scripting

diagonal matrix to square matrix

Hello, all! I am struggling with a short script to read a diagonal matrix for later retrieval. 1.000 0.234 0.435 0.123 0.012 0.102 0.325 0.412 0.087 0.098 1.000 0.111 0.412 0.115 0.058 0.091 0.190 0.045 0.058 1.000 0.205 0.542 0.335 0.054 0.117 0.203 0.125 1.000 0.587 0.159 0.357... (11 Replies)
Discussion started by: yifangt
11 Replies

8. Shell Programming and Scripting

Is it possible to draw table/matrix using shell script?

Hi all, I need to create a matrix of variable rows and columns. Right now i have 3 rows and two columns and following values. Output something like TypeA TypeB TestCase1 Pass Fail TestCase2 Pass ... (2 Replies)
Discussion started by: jakSun8
2 Replies
Login or Register to Ask a Question