Transform a listing into a table

56 Views Asked by At

I would like create a table out of listing. This is the input:

Cluster 0
Sequenz 1 
Sequenz 2
Cluster 1
Sequenz 3
Cluster 2
Sequenz 4
sequenz 5
Sequenz 6
Sequenz 7

And i want the output to be in tabular format where column one starts with the heading label "Cluster 0" and in the rows below there are sequenz 1 and 2. The next column starts with "Cluster 1" and has the Sequenz 3 in the row below and so on.

What approach and language would be best for this?

1

There are 1 best solutions below

0
On

Its not clear how you like the output to be. Some like this?

awk '
/Cluster/ {
    c=$2
    col++
    b=1
    next}
    {
    data[c FS b++]=$2
    row=row<b?b:row} 

END {
    for (i=0;i<col;i++) 
        printf "%-12s","cluster "i
    print ""
    for (j=1;j<row;j++) {
        for (i=0;i<col;i++) printf "%-12s",data[i FS j]
        print ""}
    }
' file

cluster 0   cluster 1   cluster 2
1           3           4
2                       5
                        6
                        7

This stores all data in to an array data then print it back out.