Sponsored Content
Top Forums Shell Programming and Scripting Help for a Perl newcomer! Transposing data from columns to rows Post 302578103 by durden_tyler on Wednesday 30th of November 2011 09:17:24 PM
Old 11-30-2011
Here's a different take on this pivot problem -

Code:
$
$
$ # display the contents of the input data file
$
$ cat input
Subject  Condition     Fp1            Fp2           FpZ
 S1     mixedneg    235.012        125.65        548.233
 S1     mixedneut   212.1          548.215      1548.2
 S1     pureneg     1578.22         15.25       1654.2
 S1     pureneut    4878.2         512.448       545.88
 S2     mixedneg    5465         87465         46984
 S2     mixedneut    545          7498          5136
 S2     pureneg      587          8798          6516
 S2     pureneut     874          8730.39        879
$
$
$ # Perl script to pivot the input data
$
$ perl -ane 'BEGIN {$fmt = "%-15s"}
            if ($. == 1) {
              $sub = $F[0];
              @items = @F[2..$#F];
            } elsif ( grep { $F[0] eq $x[$_][0] } 0..$#x ) {
              push @{$x[$#x]}, [ $F[1], [ @F[2..$#F] ] ] ;
            } else {
              push @x, [ $F[0], [ $F[1], [ @F[2..$#F] ] ] ];
            }
            END {
              printf ($fmt, $sub);
              foreach $itm (@items) {
                foreach $i ( 1.. $#{$x[0]} ) {
                  printf ($fmt, "$itm$x[0][$i][0]");
                }
              }
              print "\n";
              foreach $i ( 0..$#x ) {
                printf ($fmt, $x[$i][0]);
                foreach $k ( 0.. $#{$x[0][1][1]} ) {
                  foreach $j ( 1..$#{$x[$i]} ) {
                    printf ($fmt, $x[$i][$j][1][$k]);
                  }
                }
                print "\n";
              }
            }
           ' input
Subject        Fp1mixedneg    Fp1mixedneut   Fp1pureneg     Fp1pureneut    Fp2mixedneg    Fp2mixedneut   Fp2pureneg     Fp2pureneut    FpZmixedneg    FpZmixedneut   FpZpureneg     FpZpureneut
S1             235.012        212.1          1578.22        4878.2         125.65         548.215        15.25          512.448        548.233        1548.2         1654.2         545.88
S2             5465           545            587            874            87465          7498           8798           8730.39        46984          5136           6516           879
$
$
$

Each column in the output has a width of 15 characters. You can control this width by changing the value of "$fmt" variable in the BEGIN section. For example, the output for "$fmt" value equal to "%-20s" is as follows -

Code:
$
$
$ perl -ane 'BEGIN {$fmt = "%-20s"}
            if ($. == 1) {
              $sub = $F[0];
              @items = @F[2..$#F];
            } elsif ( grep { $F[0] eq $x[$_][0] } 0..$#x ) {
              push @{$x[$#x]}, [ $F[1], [ @F[2..$#F] ] ] ;
            } else {
              push @x, [ $F[0], [ $F[1], [ @F[2..$#F] ] ] ];
            }
            END {
              printf ($fmt, $sub);
              foreach $itm (@items) {
                foreach $i ( 1.. $#{$x[0]} ) {
                  printf ($fmt, "$itm$x[0][$i][0]");
                }
              }
              print "\n";
              foreach $i ( 0..$#x ) {
                printf ($fmt, $x[$i][0]);
                foreach $k ( 0.. $#{$x[0][1][1]} ) {
                  foreach $j ( 1..$#{$x[$i]} ) {
                    printf ($fmt, $x[$i][$j][1][$k]);
                  }
                }
                print "\n";
              }
            }
           ' input
Subject             Fp1mixedneg         Fp1mixedneut        Fp1pureneg          Fp1pureneut         Fp2mixedneg         Fp2mixedneut        Fp2pureneg          Fp2pureneut         FpZmixedneg         FpZmixedneut        FpZpureneg          FpZpureneut
S1                  235.012             212.1               1578.22             4878.2              125.65              548.215             15.25               512.448             548.233             1548.2              1654.2              545.88
S2                  5465                545                 587                 874                 87465               7498                8798                8730.39             46984               5136                6516                879
$
$

The order of the "FP"+"Condition" columns at the top is preserved as in the input data file. The floating point numbers correspond to their pivoted row/column pairs.

tyler_durden
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Rows to columns transposing and reformating.

----File attached. Input file =========== COL_1 <IP Add 1> COL_2 <Service1> COL_3 <ABCDEFG> COL_4 <IP ADD:PORT> COL_4 <IP ADD:PORT> COL_1 <IP Add 2> COL_2 <Service2> COL_2 <Service3> COL_2 <Service4> COL_3 <AAAABBB> COL_4 <IP ADD:PORT> COL_4 <IP ADD:PORT> COL_4 <IP... (27 Replies)
Discussion started by: bluethunder
27 Replies

2. Shell Programming and Scripting

Data in Rows to Columns

Hi, I am a beginner in bash&perl. I have data in form of:- A 1 B 2 C 3 D 4 E 5 I would like your help to find a simple way to change it to :- A B C D E 1 2 3 4 5 Any help would be highly appreciated. (8 Replies)
Discussion started by: umaars
8 Replies

3. Shell Programming and Scripting

Transposing rows into columns

I have a file like the one given below P1|V1|V2 P1|V1|V3 P1V1|V2 P2|V1|V4 P2|V2|V6 P2|V1|V4 I want it convert to P1|V1|V2|V2|V3 P2|V1|V4|V2|V6 2nd and 3rd column should be considered as together and so the tird row is duplicate Any ideas? (3 Replies)
Discussion started by: prasperl
3 Replies

4. UNIX for Dummies Questions & Answers

Suggestion to convert data in rows to data in columns

Hello everyone! I have a huge dataset looking like this: nameX nameX 0 1 2 2 2 2 2 2 2 2 2 2 2 2 1 2 2 2 1 2 2 2 ............... nameY nameY 2 2 2 2 2 2 2 2 2 2 1 2 2 2 1 2 2 2 ..... nameB nameB 0 1 2 2 2 2 2 2 2 2 1 2 2 2 1 2 2 2 ..... (can be several thousands of codes) and I need... (8 Replies)
Discussion started by: kush
8 Replies

5. Shell Programming and Scripting

awk, string as record separator, transposing rows into columns

I'm working on a different stage of a project that someone helped me address elsewhere in these threads. The .docs I'm cycling through look roughly like this: 1 of 26 DOCUMENTS Copyright 2010 The Age Company Limited All Rights Reserved The Age (Melbourne, Australia) November 27, 2010... (9 Replies)
Discussion started by: spindoctor
9 Replies

6. Shell Programming and Scripting

Transposing Repeated Rows to Columns.

I have 1000s of these rows that I would like to transpose to columns. However I would like the transpose every 3 consecutive rows to columns like below, sorted by column 3 and provide a total for each occurrences. Finally I would like a grand total of column 3. 21|FE|41|0B 50\65\78 15... (2 Replies)
Discussion started by: ravzter
2 Replies

7. Shell Programming and Scripting

Transpose Data from Columns to rows

Hello. very new to shell scripting and would like to know if anyone could help me. I have data thats being pulled into a txt file and currently have to manually transpose the data which is taking a long time to do. here is what the data looks like. Server1 -- Date -- Other -- value... (7 Replies)
Discussion started by: Mikes88
7 Replies

8. Shell Programming and Scripting

transposing columns into rows

Hi, I need to transpose columns of my files into rows and save it as individual files. sample contents of the file below. 0.9120 0.7782 0.6959 0.6904 0.6322 0.8068 0.9082 0.9290 0.7272 0.9870 0.7648 0.8053 0.8300 0.9520 0.8614 0.6734 0.7910 0.6413 0.7126 0.7364 0.8491 0.8868 0.7586 0.8949... (8 Replies)
Discussion started by: ida1215
8 Replies

9. Shell Programming and Scripting

Transposing rows and columns (pivoting) using shell scripting

Here is the contents of an input file. A,1,2,3,4 10,aaa,bbb,ccc,ddd 11,eee,fff,ggg,hhh 12,iii,jjj,lll,mmm 13,nnn,ooo,ppp I wanted the output to be A 10 1 aaa 10 2 bbb 10 3 ccc 10 4 ddd 11 1 eee 11 2 fff 11 3 ggg 11 4 hhh ..... and so on How to do it in ksh... (9 Replies)
Discussion started by: ksatish89
9 Replies

10. Shell Programming and Scripting

Transposing rows to columns with multiple similar lines

Hi, I am trying to transpose rows to columns for thousands of records. The problem is there are records that have the same lines that need to be separated. the input file as below:- ID 1A02_HUMAN AC P01892; O19619; P06338; P10313; P30444; P30445; P30446; P30514; AC Q29680; Q29837;... (2 Replies)
Discussion started by: redse171
2 Replies
All times are GMT -4. The time now is 04:56 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy