tab-separated file to matrix conversion


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers tab-separated file to matrix conversion
# 1  
Old 10-18-2011
tab-separated file to matrix conversion

hello all,

i have an input file like that

A A X0
A B X1
A C X2
...
A Z Xx
B A X1
B B X3
....
Z A Xx
Z B X4

and i want to have an output like that

A B C D
A X0 X1 X2 Xy
B X1 X3 X4
C X2 X4 Xa
D Xy

note that all X0,1,2,a... correspond to numbers, whereas A,B,C correspond to characters. Is there a way to do this in bash?
# 2  
Old 10-18-2011
The problem doing this in bash is that every single value needs to be stored in order to do this, shell memory can be limited in size, and proper two-dimensional arrays aren't available. awk is probably better suited.

writing something.

---------- Post updated at 10:47 AM ---------- Previous update was at 10:46 AM ----------

I presume this is 'battleship' style notation, row then column then value? Are they always in order like that, AA, AB, AC, BA, BB, BC... Are they actually characters, or are they longer strings? Is the number of rows and columns known?
# 3  
Old 10-18-2011
Hello Corona,

1) yes, row-column-value.
2) yes, their order is always like that
3) each is one word, composed of 3 letters.
4) yes, each file has 400 lines, meaning that the ending matrix should be 20X20...
# 4  
Old 10-18-2011
Code:
$ cat matrix.awk

BEGIN { I=1; OFS="\t" }

NF>1 {
        # If we haven't seen this index before, save its order
        if(! K[$1])
        {
                O[I]=$1;
                K[$1]=I++;
        }

        A[$1, $2]=$3;
}

END {
        for(N=1; N<I; N++)      printf("\t%s", O[N]);
        printf("\n");

        for(N=1; N<I; N++)
        {
                printf("%s", O[N]);

                for(M=1; M<I; M++)
                        printf("\t%s", A[O[N],O[M]]);

                printf("\n");
        }
}

$ cat matrix

aaa aaa 9
aaa bbb 10
aaa ccc 11
aaa ddd 12
bbb aaa 13
bbb bbb 14
bbb ccc 15
bbb ddd 16
ccc aaa 17
ccc bbb 18
ccc ccc 19
ccc ddd 20
ddd aaa 21
ddd bbb 22
ddd ccc 23
ddd ddd 24

$ awk -f matrix.awk < matrix

        aaa     bbb     ccc     ddd
aaa     9       10      11      12
bbb     13      14      15      16
ccc     17      18      19      20
ddd     21      22      23      24

$

This User Gave Thanks to Corona688 For This Post:
# 5  
Old 10-18-2011
nice code and clear. Thanks a lot!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read a tab separated file with empty column

Hi all, I'm trying to read a tab separated file and apply some functions on each column. I have an issue with empty column. Exemple: $ #cat with the sed to allow you to see my tab $ cat foo.txt| sed 's/\t/;/g' a;1;x b;;yI wanted to something like that: while read col1 col2 col3 do ... (4 Replies)
Discussion started by: maturix
4 Replies

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

3. Shell Programming and Scripting

Convert a 3 column tab delimited file to a matrix

Hi all, I have a 3 columns input file like this: CPLX9PC-4943 CPLX9PC-4943 1 CPLX9PC-4943 CpxID123 0 CPLX9PC-4943 CpxID126 0 CPLX9PC-4943 CPLX9PC-5763 0.5 CPLX9PC-4943 CpxID13 0 CPLX9PC-4943 CPLX9PC-6163 0 CPLX9PC-4943 CPLX9PC-6164 0.04... (7 Replies)
Discussion started by: AshwaniSharma09
7 Replies

4. Shell Programming and Scripting

How to replace & with and in tab separated file?

Hi, I have a tab separated. I want to replace all the "&" in 8th column of the file with "and" .I am trying with awk -F, -vOFS=\\t '{$8=($8=="&")?"and":$8}1' test> test1.txt My file is abc def ghk hjk lkm hgb jkluy acvf & bhj hihuhu fgg me mine he her go went has has & had hgf hgy ... (1 Reply)
Discussion started by: jagdishrout
1 Replies

5. Shell Programming and Scripting

conversion: 3 columns into matrix

Hi guys, here https://www.unix.com/shell-programming-scripting/193043-3-column-csv-correlation-matrix-awk-perl.html I found awk script converting awk '{ OFS = ";" if (t) { if (l != $1) t = t OFS $1 } else t = OFS $1 x = x ? x OFS $NF : $NF l = $1 }... (2 Replies)
Discussion started by: grincz
2 Replies

6. Shell Programming and Scripting

Problem with a tab separated file

Hi, I have created a tab separated file from the following input file. ADDRESS1 CITY STATE POSTAL COUNTRY LON LAT 32 PRINZREGENTENSTRASSE ROSENHEIM BAYERN 83022 DEU 1212182 4785699 263 VIA DANTE ALIGHIERI BARI PUGLIA 70122 ITA 1686233 4112154 30 VIA MILANO ... (1 Reply)
Discussion started by: ramky79
1 Replies

7. UNIX for Dummies Questions & Answers

Filling a tab-separated file with known missing entries in columns

Hello all, I have a file which is tab separated like that: PHE_205_A TIP_127_W ARG_150_B MET_1150_A TIP_12_W VAL_11_B GLU_60_A TIP_130_W ARG_143_B LEU_1033_A TIP_203_W ARG_14_B SER_1092_A TIP_203_W THR_1090_A TIP_203_W SER_1092_A TIP_25_W ... (6 Replies)
Discussion started by: TheTransporter
6 Replies

8. Shell Programming and Scripting

Convert a tab separated file using bash

Dear all, I have a file in this format (like a matrix) - A B C .. X A 1 4 2 .. 2 B 2 6 4 .. 8 C 3 5 5 .. 4 . . . ... . X . . ... . and want to convert it into a file with this format: A A = 1 A B = 4 A C = 2 ... A X = 2 B A = 2 B B = 6 etc (2 Replies)
Discussion started by: TheTransporter
2 Replies

9. UNIX for Dummies Questions & Answers

Sum up a decimal column in a tab separated text file and error handling

Hi, I have a small requirement where i need to sum up a column in a text file. Input file 66ab 000000 534385 -00000106350.00 66cd 000000 534485 -00013364511.00 66ad 000000 534485 -00000426548.00 672a 000000 534485 000000650339.82... (5 Replies)
Discussion started by: pssandeep
5 Replies

10. Shell Programming and Scripting

parse file into tab separated columns

Hello, I am trying to parse a file that resembles the last three groupings into something looking like the first two lines. I've fiddled with sed and awk a bit, but can't get anything to work properly. I need them separated by some delimiter. The file is some 23,000 lines of the stuff.... ... (9 Replies)
Discussion started by: dkozel
9 Replies
Login or Register to Ask a Question