Row to column transpose


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Row to column transpose
# 1  
Old 04-17-2013
Row to column transpose

Hi there,

Below is sample three rows which i need transpose into multiple rows.

By keeping first 2 fields static and split them into multiple rows depend following date field. Each into seperate rows.

Sample code:

Code:
	667528417,017804259,06/01/2010,05/02/2010,05/03/2010,06/04/2010,06/05/2010
	346787889,098454545,05/07/2011,08/05/2012,02/03/2012
	243455459,045464575,02/08/2010,12/09/2012


Expected Output:

Code:
667528417,017804259,06/01/2010
667528417,017804259,05/02/2010
667528417,017804259,05/03/2010
667528417,017804259,06/04/2010
667528417,017804259,06/05/2010
346787889,098454545,05/07/2011
346787889,098454545,08/05/2012
346787889,098454545,02/03/2012
243455459,045464575,02/08/2010
243455459,045464575,12/09/2012

Any help in this regards would be highly appreciated.

I have very large file need to transpose as the above requirement.

Thanks.
# 2  
Old 04-17-2013
Code:
$ cat awk.txt
BEGIN { FS = OFS = "," }
{ for (fld = 3; fld <= NF; fld++) {
  print $1, $2, $fld
  }
}

Code:
$ awk -f awk.txt sample
667528417,017804259,06/01/2010
667528417,017804259,05/02/2010
667528417,017804259,05/03/2010
667528417,017804259,06/04/2010
667528417,017804259,06/05/2010
346787889,098454545,05/07/2011
346787889,098454545,08/05/2012
346787889,098454545,02/03/2012
243455459,045464575,02/08/2010
243455459,045464575,12/09/2012

This User Gave Thanks to hanson44 For This Post:
# 3  
Old 04-17-2013
Thanks hanson44.

But for below one; Its has failed.

With Error

awk: record `829454378,111195680,...' has too many fields

Code:
829454378,111195680,25/03/2010,26/03/2010,29/03/2010,30/03/2010,31/03/2010,01/04/2010,06/04/2010,07/04/2010,08/04/2010,09/04/2010,12/04/2010,13/04/2010,14/04
/2010,15/04/2010,16/04/2010,19/04/2010,20/04/2010,21/04/2010,22/04/2010,23/04/2010,26/04/2010,27/04/2010,28/04/2010,29/04/2010,30/04/2010,03/05/2010,04/05/20
10,05/05/2010,06/05/2010,07/05/2010,10/05/2010,11/05/2010,12/05/2010,13/05/2010,14/05/2010,17/05/2010,18/05/2010,19/05/2010,20/05/2010,21/05/2010,24/05/2010,
25/05/2010,26/05/2010,27/05/2010,28/05/2010,31/05/2010,01/06/2010,02/06/2010,03/06/2010,04/06/2010,07/06/2010,08/06/2010,09/06/2010,10/06/2010,11/06/2010,14/
06/2010,15/06/2010,16/06/2010,17/06/2010,18/06/2010,21/06/2010,22/06/2010,23/06/2010,24/06/2010,25/06/2010,28/06/2010,29/06/2010,30/06/2010,01/07/2010,02/07/
2010,05/07/2010,06/07/2010,07/07/2010,08/07/2010,09/07/2010,12/07/2010,13/07/2010,14/07/2010,15/07/2010,16/07/2010,19/07/2010,20/07/2010,21/07/2010,22/07/201
0,23/07/2010,26/07/2010,27/07/2010,28/07/2010,29/07/2010,30/07/2010,02/08/2010,03/08/2010,04/08/2010,05/08/2010,06/08/2010,09/08/2010,10/08/2010,11/08/2010,1
2/08/2010,13/08/2010,16/08/2010,17/08/2010,18/08/2010,19/08/2010,20/08/2010,23/08/2010,24/08/2010,25/08/2010,26/08/2010,27/08/2010,30/08/2010,31/08/2010,01/0
9/2010,02/09/2010,03/09/2010,06/09/2010,07/09/2010,08/09/2010,09/09/2010,10/09/2010,13/09/2010,14/09/2010,15/09/2010,16/09/2010,17/09/2010,20/09/2010,21/09/2
010,22/09/2010,23/09/2010,24/09/2010,27/09/2010,28/09/2010,29/09/2010,30/09/2010,01/10/2010,04/10/2010,05/10/2010,06/10/2010,07/10/2010,08/10/2010,11/10/2010
,12/10/2010,13/10/2010,14/10/2010,15/10/2010,18/10/2010,19/10/2010,20/10/2010,21/10/2010,22/10/2010,25/10/2010,26/10/2010,27/10/2010,28/10/2010,29/10/2010,01
/11/2010,02/11/2010,03/11/2010,04/11/2010,05/11/2010,08/11/2010,09/11/2010,10/11/2010,11/11/2010,12/11/2010,15/11/2010,16/11/2010,17/11/2010,18/11/2010,19/11
/2010,22/11/2010,23/11/2010,24/11/2010,25/11/2010,26/11/2010,29/11/2010,30/11/2010,01/12/2010,02/12/2010,03/12/2010,06/12/2010,07/12/2010,08/12/2010,09/12/20
10,10/12/2010,13/12/2010,14/12/2010,15/12/2010,16/12/2010,17/12/2010,20/12/2010,21/12/2010,22/12/2010,23/12/2010,24/12/2010,29/12/2010,30/12/2010,31/12/2010

# 4  
Old 04-17-2013
It seems like there are 200 fields in that record. That does not seem that many fields. It runs fine here. I use gawk (GNU awk) on Linux. Is there any chance you are using the original awk version (old awk)? If yes, maybe that awk version has a limitation for number of fields, and any chance your computer has nawk (new awk) or gawk (GNU awk)? Most do.
Code:
$ awk -f awk.txt sample | head -4
829454378,111195680,25/03/2010
829454378,111195680,26/03/2010
829454378,111195680,29/03/2010
829454378,111195680,30/03/2010

Code:
$ awk -f awk.txt sample | tail -4
829454378,111195680,24/12/2010
829454378,111195680,29/12/2010
829454378,111195680,30/12/2010
829454378,111195680,31/12/2010

---------- Post updated at 03:10 AM ---------- Previous update was at 02:45 AM ----------

Here's a way that does not depend on awk # of fields.
Code:
$ cat temp.sh
sed "s/^/BEGIN_LINE,/" sample > temp1.x
tr "," "\n" < temp1.x > temp2.x
while read line; do
  if [ $line = "BEGIN_LINE" ]; then
    read field1
    read field2
    continue
  fi
  echo $field1,$field2,$line
done < temp2.x

With original input:
Code:
$ ./temp.sh
667528417,017804259,06/01/2010
667528417,017804259,05/02/2010
667528417,017804259,05/03/2010
667528417,017804259,06/04/2010
667528417,017804259,06/05/2010
346787889,098454545,05/07/2011
346787889,098454545,08/05/2012
346787889,098454545,02/03/2012
243455459,045464575,02/08/2010
243455459,045464575,12/09/2012

With really long line:
Code:
$ ./temp.sh | head -4
829454378,111195680,25/03/2010
829454378,111195680,26/03/2010
829454378,111195680,29/03/2010
829454378,111195680,30/03/2010

Code:
$ ./temp.sh | tail -4
829454378,111195680,24/12/2010
829454378,111195680,29/12/2010
829454378,111195680,30/12/2010
829454378,111195680,31/12/2010

Of course, if the input line is REALLY long, sed also would have a problem with it. If that happens, there is another solution, but no use going into that if the above shell script works, or if you can get an awk that allows more fields.

BTW, I tested gawk with 7198 fields, and no problem. I don't know what the maximum is.
This User Gave Thanks to hanson44 For This Post:
# 5  
Old 04-17-2013
Thanks hanson44. SED script helped us.

We don't have gawk,nawk in our env.

Thanks
# 6  
Old 04-18-2013
Quote:
Originally Posted by ganeshd
Hi there,&lt;br /&gt;
&lt;br /&gt;
Below is sample three rows which i need transpose into multiple rows.&lt;br /&gt;
&lt;br /&gt;
By keeping first 2 fields static and split them into multiple rows depend following date field. Each into seperate rows.&lt;br /&gt;
&lt;br /&gt;
Sample code:&lt;br /&gt;
&lt;br /&gt;
Code:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;667528417,017804259,06/01/2010,05/02/2010,05/03/2010,06/04/2010,06/05/2010&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;346787889,098454545,05/07/2011,08/05/2012,02/03/2012&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;243455459,045464575,02/08/2010,12/09/2012

&lt;br /&gt;
Expected Output:&lt;br /&gt;
&lt;br /&gt;
Code:
667528417,017804259,06/01/2010&lt;br /&gt;
667528417,017804259,05/02/2010&lt;br /&gt;
667528417,017804259,05/03/2010&lt;br /&gt;
667528417,017804259,06/04/2010&lt;br /&gt;
667528417,017804259,06/05/2010&lt;br /&gt;
346787889,098454545,05/07/2011&lt;br /&gt;
346787889,098454545,08/05/2012&lt;br /&gt;
346787889,098454545,02/03/2012&lt;br /&gt;
243455459,045464575,02/08/2010&lt;br /&gt;
243455459,045464575,12/09/2012

Any help in this regards would be highly appreciated.&lt;br /&gt;
&lt;br /&gt;
I have very large file need to transpose as the above requirement.&lt;br /&gt;
&lt;br /&gt;
Thanks.
&lt;br /&gt;
&lt;br /&gt;
bài viết của bạn hay lắm --cam ơn bạn đă chia sẽ . thanks
# 7  
Old 04-18-2013
Try this ...

Code:
for i in `cat a.txt`
do 
 num=`echo "$i" |awk -F',' '{print $1 "," $2}'`
   for j in `echo "$i"|tr ',' '\n'|sed -n  '3,$p'`
    do
      echo  $num","$j
   done
done

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Transpose the Row and column

Hi, I have data in form of A ram B shyam C seeta D geeta A bob B methew C Richad D Mike and i want it in this form. A B C D ram shyam seeta geeta bob methew Richard Mike. please help by providing the scripting for this. (3 Replies)
Discussion started by: ricbha
3 Replies

2. Shell Programming and Scripting

How transpose column in a row?

Hello guys, First of all happy holidays and happy new year. I'm new in bioinformatic and also it is my first time that I write in this forum. Therefore, sorry if I make some mistakes. I'm writing to ask your help to fix a problem: I have a file like this: gene1 GO:0016491|GO:0055114... (8 Replies)
Discussion started by: Salvatore_espos
8 Replies

3. Shell Programming and Scripting

Transpose from row to column using timestamp in first column

Gents, Transpose from row to column, taking in consideration the first column, which contends the date. Input file 72918,111000009,111000009,111000009,111000009,111000009,111000009,111000009,111000009,111000009 72918,2356,2357,2358,2359,2360,2361,2362,2363,2364 72918,0,0,0,0,0,0,0,0,0... (12 Replies)
Discussion started by: jiam912
12 Replies

4. Shell Programming and Scripting

Transpose row to column

I'm using the testawk.awk from the following thread https://www.unix.com/shell-programming-and-scripting/18897-row-column-transpose.htmlI'm getting the following output fieldname1 data1 fieldname2 data2 fieldname3 data3 How can I get like this instead 1 fieldname1 data1 2 fieldname2 data2... (1 Reply)
Discussion started by: makkan
1 Replies

5. Shell Programming and Scripting

To transpose row into column

Hi All, In shell, I have below data coming from some some text file as below: . 351706 5861.8 0.026 0.012 12.584 0.026 0.012 12.582 0.000 0.000 0.000 Now i need the above data to be transposed as below 351706... (16 Replies)
Discussion started by: Anamica
16 Replies

6. Shell Programming and Scripting

Transpose column to row - awk

Hi there, I have a small csv file example below: source,cu_001,cu_001_volume,cu_001_mass,cu_002,cu_002_volume,cu_002_mass,cu_003,cu_003_volume,cu_003_mass ja116,1.33,3024000,9374400,1.54,3026200,9375123,1.98,3028000,9385512 I want to transpose columns to rows starting at the second... (3 Replies)
Discussion started by: theflamingmoe
3 Replies

7. Shell Programming and Scripting

Column to row Transpose

Hi Folks, Iam a kinda newbie to unix shell scripting, the scenario is i have a text file containing the following info Charlie chicago 15 Charlie newyork 26 jonny chicago 14 jonny newyork 15 joe chicago 15 joe newyork 18output should be Name chicago ... (3 Replies)
Discussion started by: tech_frk
3 Replies

8. Shell Programming and Scripting

Row to column transpose between same pattern.

Hi All, I have been trying to transpose rows to column in an large file (about 15000 lines) between matching pattern. Searched all posts in this forum, but not able find the solution to my problem. Any help appreciated.!! Input /*------XXXXXX-------*/ owner: XXXX location: XXXX... (3 Replies)
Discussion started by: RobP
3 Replies

9. Shell Programming and Scripting

Transpose column to row

Hi i have a file which has values seperated by "," as shown below and I want to transpose for every doc_id in one row. Input: DOC_ID,KEYWORD 105,REGISTROS 105,GEOLOGIA 105,NUCLEOS 105,EXPEDIENTE 105,PROGRAMAS 10025,EXPEDIENTE 10025,LOCALIZACIONES 10025,OFICIOS 10025,PROGRAMAS... (4 Replies)
Discussion started by: juelillo
4 Replies

10. Shell Programming and Scripting

Row to column transpose

Can we transpose rows to columns? Fields within row are separated by a comma. (10 Replies)
Discussion started by: videsh77
10 Replies
Login or Register to Ask a Question