transposing columns into rows


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting transposing columns into rows
# 1  
Old 02-03-2012
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.
Code:
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 0.6883 0.6798 0.6966 0.9124 

desired output:
Code:
outfile1: outfile2:  outfile3: outfile4:
0.9120   0.9290    0.8614    0.8868
0.7782   0.7272    0.6734    0.7586
0.6959   0.9870    0.7910    0.8949
0.6904   0.7648    0.6413    0.6883
0.6322   0.8053    0.7126    0.6798
0.8068   0.8300    0.7364    0.6966
0.9082   0.9520    0.8491    0.9124

bunch of thanks in advance.
# 2  
Old 02-03-2012
paste

Have you looked at the paste command?

From your example, unclear if 'original' is one file or four files.
# 3  
Old 02-03-2012
Hi, the original file is a single file. I need to transpose the columns of each row into columns and save it into individual files.thanks
# 4  
Old 02-03-2012
GIYF. (Google is your friend.)

Have a look at
unstableme.blogspot.com/2008/08/row-to-column-transpose-bash-scripting.html

And just google "Unix script Transpose" or maybe add ant specific shell or tool you want to use. Like ksh, or perl, etc.

Enjoy!
# 5  
Old 02-03-2012
Code:
perl -lane 'BEGIN{$i=1}open O,">> output_$i";for(@F){print O}close O;$i++' inputfile

# 6  
Old 02-03-2012
Code:
[root@node2 ~]# cat transpose 
#!/bin/bash 
 
transpose() 
{ 
  awk ' 
      { 
              if (max_nf<NF) 
                    max_nf=NF 
              max_nr=NR 
              for (x=1; x<=NF; ++x) 
                     vector[x, NR]=$x 
      } 
 
      END { 
              for (x=1; x<=max_nf; ++x) { 
                   for (y=1; y<=max_nr; ++y) 
                        printf("%s ", vector[x, y]) 
                   printf("\n") 
               } 
          }'  ${1} 
} 
 
transpose ${1} 

[root@node2 ~]# cat data 
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 0.6883 0.6798 0.6966 0.9124 
[root@node2 ~]# bash transpose data 
0.9120 0.9290 0.8614 0.8868 
0.7782 0.7272 0.6734 0.7586 
0.6959 0.9870 0.7910 0.8949 
0.6904 0.7648 0.6413 0.6883 
0.6322 0.8053 0.7126 0.6798 
0.8068 0.8300 0.7364 0.6966 
0.9082 0.9520 0.8491 0.9124

to get your file:
Code:
for i in {1..4}; do 
    bash transpose data | awk -v row=$i '{print $row > "outfile"row}'
done

or

Code:
[root@node2 ~]# cat run 
#!/bin/bash

Transpose_matrix()
{
nr=$(wc -l < ${1})
nf=$(head -n 1 ${1} | xargs -n 1 | wc -l)
for((NF=1; NF<=${nf}; ++NF))
do
    cut -d ' ' -f ${NF} ${1}
done | xargs -n ${nr}
}

Transpose_matrix ${1}
[root@node2 ~]# bash run data 
0.9120 0.9290 0.8614 0.8868
0.7782 0.7272 0.6734 0.7586
0.6959 0.9870 0.7910 0.8949
0.6904 0.7648 0.6413 0.6883
0.6322 0.8053 0.7126 0.6798
0.8068 0.8300 0.7364 0.6966
0.9082 0.9520 0.8491 0.9124

to get your file:
Code:
for i in {1..4}; do 
    bash run data | awk -v row=$i '{print $row > "outfile"row}'
done

or
Code:
[root@node2 ~]# awk '{print $0 | "xargs -n 1 > outfile"NR }' data


Last edited by complex.invoke; 02-03-2012 at 09:36 PM..
# 7  
Old 02-04-2012
Try:
Code:
awk '{f="outfile"NR;$1=$1;print>f;close(f)}' OFS="\n" infile


Last edited by Scrutinizer; 02-04-2012 at 03:44 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

2. Shell Programming and Scripting

Awk/sed script for transposing any number of rows with header row

Greetings! I have been trying to find out a way to take a CSV file with a large number of rows, and a very large number of columns (in the thousands) and convert the rows to a single column of data, where the first row is a header representing the attribute name and the subsequent series of... (3 Replies)
Discussion started by: tntelle
3 Replies

3. Shell Programming and Scripting

transposing lines to columns

Okay folks, here's a question. I tried searching but couldn't find exactly what I needed. I have a text file (excerpt below). This text file is an extract I did from several hundred pages of datasheets using grep so I could look only at the site history for each site. The problem is that... (2 Replies)
Discussion started by: jbrandt1979
2 Replies

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

5. Shell Programming and Scripting

Help for a Perl newcomer! Transposing data from columns to rows

I have to create a Perl script which will transpose the data output from my experiment, from columns to rows, in order for me to analyse the data. I am a complete Perl novice so any help would be greatly appreciated. The data as it stands looks like this: Subject Condition Fp1 ... (12 Replies)
Discussion started by: Sarah_W
12 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

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

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

9. Shell Programming and Scripting

Transposing columns with awk

I want a sweet simple time efficient awk script in online which gets output 001_r 0.0265185 0.0437049 0.0240642 0.0310264 0.0200482 0.0146746 0.0351344 0.0347856 0.036119 1.49 firstcoloumnvalue allvaluesof 'c' in one row 001_r : 002_r c: 0.0265185 N: 548 001_r : 007_r c:... (5 Replies)
Discussion started by: phoenix_nebula
5 Replies

10. 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
Login or Register to Ask a Question