Row blocks to column blocks


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Row blocks to column blocks
# 1  
Old 05-14-2013
Row blocks to column blocks

Hello,
Searched for a while and found some "line-to-column" script. My case is similar but with multiple fields each row:
Code:
S02     Length    Per
S02     7043      3.864
S02     54477    29.89
S02     104841    57.52
S03     Length    Per
S03     1150    0.835
S03     1321    0.96
S03     6432    4.675
S03     6922    5.031
S03     11705    8.507
S03     13793    10.02
S03     34275    24.91
S03     38238    27.79
S04    Length    Per
S04     11398    8.887
S04     17396    13.56
S04     94014    73.3

And I want the output as:
Code:
S02    Length    Per      S03    Length    Per   S04  Length    Per
S02    7043      3.864    S03    1150    0.835   S04 11398    8.887
S02    54477     29.89    S03    1321    0.96    S04 17396    13.56
S02    104841    57.52    S03    6432    4.675   S04 94014    73.3
                          S03    6922    5.031            
                          S03    11705    8.507            
                          S03    13793    10.02            
                          S03    34275    24.91            
                          S03    38238    27.79

What I tried is to cut each block (by field 1) to separate file and then use "paste" to combine the individual files. I feel there must be an easier way with awk.
Any help will be appreciated.
# 2  
Old 05-14-2013
It will not be simple since the column are not equal size.
If its not to large, I would do cut and past in excel Smilie
Can I ask why you need this output?
# 3  
Old 05-14-2013
Thanks Jotne!
Two reasons for this output.
1) I need this output for other statistics like min, max, mean, 5number etc of each block to compare the other 96 blocks in the output format;
2) There are many posts about "line-to-column" script in awk to do the similar conversion, I thought my case may be resolved with awk too, but could not figure out by myself.
I am aware of the line numbers are now equal for each block, so that it is more challenging to me.
Does this make any sense? Please give me any suggestion or comments. Thanks a lot!

Last edited by yifangt; 05-14-2013 at 06:27 PM..
# 4  
Old 05-14-2013
Code:
awk '{print >$1".tmp"}' file
paste *.tmp | expand -30


Last edited by MadeInGermany; 05-14-2013 at 06:55 PM.. Reason: "| expand"
This User Gave Thanks to MadeInGermany For This Post:
# 5  
Old 05-14-2013
Here is an awk code that might help:
Code:
awk '
        NR == FNR {
                if (!($1 in C))
                {
                        C[$1]
                        V[++k] = $1
                }
                next
        }
        p != $1 {
                c = 0
        }
        {
                A[$1","++c] = $0
                p = $1
                if ( m < c )
                        m = c
        }
        END {
                for ( i = 1; i <= m; i++ )
                {
                        for ( j = 1; j <= k; j++ )
                        {
                                printf "%s\t", A[V[j]","i] ? A[V[j]","i] : "\t\t"
                        }
                        printf "\n"
                }
        }
' file file

This User Gave Thanks to Yoda For This Post:
# 6  
Old 05-14-2013
row blocks to column

Thanks MIG and Yoda!
MIG, your code is what I was looking for.
How about if I have same header for each block as "ID Length Per"?
Code:
ID    Length    Per 
1     7043      3.864 
2     54477    29.89 
3     104841    57.52 
ID     Length    Per 
1     1150    0.835 
2     1321    0.96 
3     6432    4.675 
4     6922    5.031 
5     11705    8.507 
6     13793    10.02 
7     34275    24.91 
8     38238    27.79 
ID    Length    Per 
1     11398    8.887 
2     17396    13.56 
3     94014    73.3

which is the original data structure. I first struggled with RS, but could not figure out the way. Then I added the first field as the group ID.
Is it possible to use the RS to handle the original situation? Thanks!
# 7  
Old 05-17-2013
Quote:
Originally Posted by yifangt
How about if I have same header for each block as "ID Length Per"?...
In that case try the following awk snippet...
Code:
awk '/^Id/ {s = $0; l = length() + 6; ws = sprintf("%*s", l, " ")}
    !/^Id/ {
      a[$1]++
      max = (max > a[$1] ? max : a[$1])
      if ($1 != prev) b[++j] = $1
      c[$1,a[$1]] = $0
      prev = $1
} END {
      for (i = 1; i <=j; i++) {
          t = sprintf("%-*s", l, s)
          h = (h ? h "" t : t)
      }
      printf("%s\n", h)
      for (k = 1; k <= max; k++) {
          for (i = 1; i <= j; i++) {
              if ((f = c[b[i],k])) {
                 m = l - length(f)
                 r = sprintf("%*s", m, " ")
                 str = sprintf("%s", str ? str "" f "" r : f "" r)
              }
              else str = sprintf("%*s", l, str ? str "" ws : ws)
          }
          printf("%s\n", str) 
          str = ""
      }
}' file

where file looks like...
Code:
Id     Length    Per
S02    7043      3.864
S02    54477     29.89
S02    104841    57.52
Id     Length    Per
S03    1150      0.835
S03    1321      0.96
S03    6432      4.675
S03    6922      5.031
S03    11705     8.507
S03    13793     10.02
S03    34275     24.91
S03    38238     27.79
Id     Length    Per
S04    11398     8.887
S04    17396     13.56
S04    94014     73.3

This User Gave Thanks to shamrock For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Blocks into table

please help, I have a huge file with blocks of data which I need to convert to a tabular format. Input sample id: GO:0000017 name: alpha-glucoside transport namespace: biological_process def: "The directed movement of alpha-glucosides into, out of or within a cell, or between... (3 Replies)
Discussion started by: ritakadm
3 Replies

2. Shell Programming and Scripting

Transpose info that is within blocks

Hello to all in forum, I have a big file with blocks of data. Each block begins with "BeginOfRecord".... (17 Replies)
Discussion started by: Ophiuchus
17 Replies

3. UNIX for Dummies Questions & Answers

Duplicate blocks in an inode

I have 2 duplicate blocks in an inode and I want to get rid of one of them so that I can get into my pc. The message I get is Multiply-claimed block(s) in inode 5997500: 12690101 12690101. All help is appreciated. Thanks (7 Replies)
Discussion started by: Nighttrain
7 Replies

4. Shell Programming and Scripting

how to split this file into blocks and then send these blocks as input to the tool called Yices?

Hello, I have a file like this: FILE.TXT: (define argc :: int) (assert ( > argc 1)) (assert ( = argc 1)) <check> # (define c :: float) (assert ( > c 0)) (assert ( = c 0)) <check> # now, i want to separate each block('#' is the delimeter), make them separate files, and then send them as... (5 Replies)
Discussion started by: paramad
5 Replies

5. UNIX for Dummies Questions & Answers

Convert 512-blocks to 4k blocks

I'm Unix. I'm looking at "df" on Unix now and below is an example. It's lists the filesystems out in 512-blocks, I need this in 4k blocks. Is there a way to do this in Unix or do I manually convert and how? So for container 1 there is 7,340,032 in size in 512-blocks. What would the 4k block be... (2 Replies)
Discussion started by: rockycj
2 Replies

6. Shell Programming and Scripting

Help with Script using Command Blocks

Hello, I am trying to create a shell script that use command block (donīt really know if this is the correct way to say it), but while one version works fine, the other one is not working at all. So let me show an example of this "command block" Iīm using and its working ok: cat << _EOF_ `echo... (7 Replies)
Discussion started by: Alexis Duarte
7 Replies

7. Shell Programming and Scripting

Removing blocks from a file

I have a file like the one below. Each record is separated with > In between I have lines consisting of 3 numeric values separated by a space. I need to take each block between the > sign and read the first number in the line. Then take the first after the > sign and the last before the >... (7 Replies)
Discussion started by: kristinu
7 Replies

8. Shell Programming and Scripting

How to read text in blocks

Hi, I have file which contains information written in blocks (every block is different). Is it possible to read every block one by one to another file (one block per file). The input is something like this <block1> <empty line> <block2> <empty line> ... ... ... <block25> <empty... (0 Replies)
Discussion started by: art84_)LV
0 Replies

9. Shell Programming and Scripting

Extract sequence blocks

Hi, I have an one-line file consisting of a sequence of 660 letters. I would like to extract 9-letter blocks iteratively: ASDFGHJKLQWERTYUIOPZXCVBNM first block: ASDFGHJKL 1nd block: SDFGHJKLQ What I have so far only gives me the first block, can anyone please explain why? cat... (7 Replies)
Discussion started by: solli
7 Replies
Login or Register to Ask a Question