Transpose last column of one file to last row of second file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Transpose last column of one file to last row of second file
# 1  
Old 03-17-2014
Transpose last column of one file to last row of second file

Hello Everyone,

I'm very new to gawk and I need help transposing the last column a .csv file the last row of another .csv file. The data looks as follow. Your help will be much appreciated.

fromfile.csv

Code:
26/02/2013,27/02/2013,28/02/2013,03/03/2013,04/03/2013,05/03/2013,06/03/2013,07/03/2013,10/03/2013,11/03/2013,12/03/2013,13/03/2013,14/03/2013,17/03/2013
0.9153-0.9160,0.9105-0.9112,0.9105-0.9111,0.9080-0.9086,0.9105-0.9111,0.9080-0.9086,0.9129-0.9135,0.9227-0.9233,0.9227-0.9233,0.9153-0.9159,0.9133-0.9139,0.9133-0.9139,0.9179-0.9185,0.9153-0.9159
0.9129-0.9136,0.9080-0.9087,0.9080-0.9086,0.9044-0.9050,0.9080-0.9086,0.9044-0.9050,0.9105-0.9111,0.9204-0.9210,0.9204-0.9210,0.9133-0.9139,0.9111-0.9117,0.9111-0.9117,0.9153-0.9159,0.9133-0.9139
0.9105-0.9112,0.9044-0.9051,0.9044-0.9050,0.9026-0.9032,0.9044-0.9050,0.9026-0.9032,0.9080-0.9086,0.9179-0.9185,0.9179-0.9185,0.9111-0.9117,0.9087-0.9093,0.9087-0.9093,0.9133-0.9139,0.9103-0.9109
0.9080-0.9087,0.9026-0.9033,0.9026-0.9032,0.8989-0.8995,0.9026-0.9032,0.8989-0.8995,0.9044-0.9050,0.9153-0.9159,0.9153-0.9159,0.9087-0.9093,0.9051-0.9057,0.9051-0.9057,0.9103-0.9109,0.9076-0.9082
0.9044-0.9051,0.8997-0.9004,0.8997-0.9003,0.8960-0.8966,0.8989-0.8995,0.8969-0.8975,0.9026-0.9032,0.9113-0.9119,0.9113-0.9119,0.9063-0.9069,0.9032-0.9038,0.9032-0.9038,0.9062-0.9068,0.9049-0.9055
0.9026-0.9033,0.8968-0.8975,0.8971-0.8977,0.8939-0.8945,0.8946-0.8952,0.8946-0.8952,0.8997-0.9003,0.9091-0.9097,0.9097-0.9103,0.9034-0.9040,0.8997-0.9003,0.8993-0.8999,0.9042-0.9048,0.9030-0.9036
0.8999-0.9006,0.8940-0.8947,0.8949-0.8955,0.8912-0.8918,0.8925-0.8931,0.8927-0.8933,0.8970-0.8976,0.9060-0.9066,0.9075-0.9081,0.9008-0.9014,0.8958-0.8964,0.8959-0.8965,0.9013-0.9019,0.8993-0.8999
0.8966-0.8973,0.8901-0.8908,0.8924-0.8930,0.8869-0.8875,0.8887-0.8893,0.8906-0.8912,0.8933-0.8939,0.9031-0.9037,0.9031-0.9037,0.8969-0.8975,0.8933-0.8939,0.8920-0.8926,0.8983-0.8989,0.8959-0.8965
0.8935-0.8942,0.8869-0.8876,0.8899-0.8905,0.8824-0.8830,0.8869-0.8875,0.8887-0.8893,0.8906-0.8912,0.9001-0.9007,0.9001-0.9007,0.8933-0.8939,0.8906-0.8912,0.8887-0.8893,0.8959-0.8965,0.8920-0.8926
0.8901-0.8908,0.8824-0.8831,0.8869-0.8875,0.8796-0.8802,0.8824-0.8830,0.8869-0.8875,0.8887-0.8893,0.8969-0.8975,0.8969-0.8975,0.8906-0.8912,0.8887-0.8893,0.8869-0.8875,0.8920-0.8926,0.8887-0.8893
0.8869-0.8876,0.8796-0.8803,0.8824-0.8830,0.8768-0.8774,0.8796-0.8802,0.8824-0.8830,0.8869-0.8875,0.8933-0.8939,0.8933-0.8939,0.8887-0.8893,0.8869-0.8875,0.8846-0.8852,0.8887-0.8893,0.8869-0.8875
0.8824-0.8831,0.8768-0.8775,0.8796-0.8802,0.8727-0.8733,0.8768-0.8774,0.8796-0.8802,0.8846-0.8852,0.8906-0.8912,0.8906-0.8912,0.8869-0.8875,0.8846-0.8852,0.8824-0.8830,0.8869-0.8875,0.8846-0.8852

"Desired" tofile.csv

Code:
10/03/2013,0.9227-0.9233,0.9204-0.9210,0.9179-0.9185,0.9153-0.9159,0.9113-0.9119,0.9097-0.9103,0.9075-0.9081,0.9031-0.9037,0.9001-0.9007,0.8969-0.8975,0.8933-0.8939,0.8906-0.8912
11/03/2013,0.9153-0.9159,0.9133-0.9139,0.9111-0.9117,0.9087-0.9093,0.9063-0.9069,0.9034-0.9040,0.9008-0.9014,0.8969-0.8975,0.8933-0.8939,0.8906-0.8912,0.8887-0.8893,0.8869-0.8875
12/03/2013,0.9133-0.9139,0.9111-0.9117,0.9087-0.9093,0.9051-0.9057,0.9032-0.9038,0.8997-0.9003,0.8958-0.8964,0.8933-0.8939,0.8906-0.8912,0.8887-0.8893,0.8869-0.8875,0.8846-0.8852
13/03/2013,0.9133-0.9139,0.9111-0.9117,0.9087-0.9093,0.9051-0.9057,0.9032-0.9038,0.8993-0.8999,0.8959-0.8965,0.8920-0.8926,0.8887-0.8893,0.8869-0.8875,0.8846-0.8852,0.8824-0.8830
14/03/2013,0.9179-0.9185,0.9153-0.9159,0.9133-0.9139,0.9103-0.9109,0.9062-0.9068,0.9042-0.9048,0.9013-0.9019,0.8983-0.8989,0.8959-0.8965,0.8920-0.8926,0.8887-0.8893,0.8869-0.8875
17/03/2013,0.9153-0.9159,0.9133-0.9139,0.9103-0.9109,0.9076-0.9082,0.9049-0.9055,0.9030-0.9036,0.8993-0.8999,0.8959-0.8965,0.8920-0.8926,0.8887-0.8893,0.8869-0.8875,0.8846-0.8852

The need is just to transpose the last column from one file (fromfile.csv) to last row of the second file (the desired tofile.csv)

I have searched through the forum however I could not locate any relevant post, if this kind of query has already been posted before, please accept my sincere apologies.

Last edited by Hardik Panchal; 03-17-2014 at 06:52 AM.. Reason: Add CODE tags.
# 2  
Old 03-17-2014
Untested
Code:
awk '{
      for(i=1;i<=NF;i++)
      A[i] = A[i] ? A[i] OFS $i : $i
     }
  END{
	for(i=1;i<=NF;i++)
	print A[i]
     }' FS=',' OFS=',' file

# 3  
Old 03-17-2014
Code:
awk 'BEGIN{FS = OFS = ","}
 {a[NR] = $NF}
 END {for(i = 1; i <= length(a); i++)
  {out = (out == "") ? a[i] : (out OFS a[i])};
 print out}' fromfile.csv >> tofile.csv

# 4  
Old 03-17-2014
Though I could not test code for some reason, I can see that you have modified your input, here is original input you have posted, don't do like this again in future.,

Code:
$ cat foo
10/03/2013,11/03/2013,12/03/2013,13/03/2013,14/03/2013,17/03/2013
0.9227-0.9233,0.9153-0.9159,0.9133-0.9139,0.9133-0.9139,0.9179-0.9185,0.9153-0.9159
0.9204-0.9210,0.9133-0.9139,0.9111-0.9117,0.9111-0.9117,0.9153-0.9159,0.9133-0.9139
0.9179-0.9185,0.9111-0.9117,0.9087-0.9093,0.9087-0.9093,0.9133-0.9139,0.9103-0.9109
0.9153-0.9159,0.9087-0.9093,0.9051-0.9057,0.9051-0.9057,0.9103-0.9109,0.9076-0.9082
0.9113-0.9119,0.9063-0.9069,0.9032-0.9038,0.9032-0.9038,0.9062-0.9068,0.9049-0.9055
0.9097-0.9103,0.9034-0.9040,0.8997-0.9003,0.8993-0.8999,0.9042-0.9048,0.9030-0.9036
0.9075-0.9081,0.9008-0.9014,0.8958-0.8964,0.8959-0.8965,0.9013-0.9019,0.8993-0.8999
0.9031-0.9037,0.8969-0.8975,0.8933-0.8939,0.8920-0.8926,0.8983-0.8989,0.8959-0.8965
0.9001-0.9007,0.8933-0.8939,0.8906-0.8912,0.8887-0.8893,0.8959-0.8965,0.8920-0.8926
0.8969-0.8975,0.8906-0.8912,0.8887-0.8893,0.8869-0.8875,0.8920-0.8926,0.8887-0.8893
0.8933-0.8939,0.8887-0.8893,0.8869-0.8875,0.8846-0.8852,0.8887-0.8893,0.8869-0.8875
0.8906-0.8912,0.8869-0.8875,0.8846-0.8852,0.8824-0.8830,0.8869-0.8875,0.8846-0.8852

---------- Post updated at 05:14 PM ---------- Previous update was at 05:12 PM ----------

Quote:
Originally Posted by Hardik Panchal
Hello Everyone,

I'm very new to gawk and I need help transposing the last column a .csv file the last row of another .csv file. The data looks as follow. Your help will be much appreciated.

fromfile.csv

Code:
26/02/2013,27/02/2013,28/02/2013,03/03/2013,04/03/2013,05/03/2013,06/03/2013,07/03/2013,10/03/2013,11/03/2013,12/03/2013,13/03/2013,14/03/2013,17/03/2013
0.9153-0.9160,0.9105-0.9112,0.9105-0.9111,0.9080-0.9086,0.9105-0.9111,0.9080-0.9086,0.9129-0.9135,0.9227-0.9233,0.9227-0.9233,0.9153-0.9159,0.9133-0.9139,0.9133-0.9139,0.9179-0.9185,0.9153-0.9159
0.9129-0.9136,0.9080-0.9087,0.9080-0.9086,0.9044-0.9050,0.9080-0.9086,0.9044-0.9050,0.9105-0.9111,0.9204-0.9210,0.9204-0.9210,0.9133-0.9139,0.9111-0.9117,0.9111-0.9117,0.9153-0.9159,0.9133-0.9139
0.9105-0.9112,0.9044-0.9051,0.9044-0.9050,0.9026-0.9032,0.9044-0.9050,0.9026-0.9032,0.9080-0.9086,0.9179-0.9185,0.9179-0.9185,0.9111-0.9117,0.9087-0.9093,0.9087-0.9093,0.9133-0.9139,0.9103-0.9109
0.9080-0.9087,0.9026-0.9033,0.9026-0.9032,0.8989-0.8995,0.9026-0.9032,0.8989-0.8995,0.9044-0.9050,0.9153-0.9159,0.9153-0.9159,0.9087-0.9093,0.9051-0.9057,0.9051-0.9057,0.9103-0.9109,0.9076-0.9082
0.9044-0.9051,0.8997-0.9004,0.8997-0.9003,0.8960-0.8966,0.8989-0.8995,0.8969-0.8975,0.9026-0.9032,0.9113-0.9119,0.9113-0.9119,0.9063-0.9069,0.9032-0.9038,0.9032-0.9038,0.9062-0.9068,0.9049-0.9055
0.9026-0.9033,0.8968-0.8975,0.8971-0.8977,0.8939-0.8945,0.8946-0.8952,0.8946-0.8952,0.8997-0.9003,0.9091-0.9097,0.9097-0.9103,0.9034-0.9040,0.8997-0.9003,0.8993-0.8999,0.9042-0.9048,0.9030-0.9036
0.8999-0.9006,0.8940-0.8947,0.8949-0.8955,0.8912-0.8918,0.8925-0.8931,0.8927-0.8933,0.8970-0.8976,0.9060-0.9066,0.9075-0.9081,0.9008-0.9014,0.8958-0.8964,0.8959-0.8965,0.9013-0.9019,0.8993-0.8999
0.8966-0.8973,0.8901-0.8908,0.8924-0.8930,0.8869-0.8875,0.8887-0.8893,0.8906-0.8912,0.8933-0.8939,0.9031-0.9037,0.9031-0.9037,0.8969-0.8975,0.8933-0.8939,0.8920-0.8926,0.8983-0.8989,0.8959-0.8965
0.8935-0.8942,0.8869-0.8876,0.8899-0.8905,0.8824-0.8830,0.8869-0.8875,0.8887-0.8893,0.8906-0.8912,0.9001-0.9007,0.9001-0.9007,0.8933-0.8939,0.8906-0.8912,0.8887-0.8893,0.8959-0.8965,0.8920-0.8926
0.8901-0.8908,0.8824-0.8831,0.8869-0.8875,0.8796-0.8802,0.8824-0.8830,0.8869-0.8875,0.8887-0.8893,0.8969-0.8975,0.8969-0.8975,0.8906-0.8912,0.8887-0.8893,0.8869-0.8875,0.8920-0.8926,0.8887-0.8893
0.8869-0.8876,0.8796-0.8803,0.8824-0.8830,0.8768-0.8774,0.8796-0.8802,0.8824-0.8830,0.8869-0.8875,0.8933-0.8939,0.8933-0.8939,0.8887-0.8893,0.8869-0.8875,0.8846-0.8852,0.8887-0.8893,0.8869-0.8875
0.8824-0.8831,0.8768-0.8775,0.8796-0.8802,0.8727-0.8733,0.8768-0.8774,0.8796-0.8802,0.8846-0.8852,0.8906-0.8912,0.8906-0.8912,0.8869-0.8875,0.8846-0.8852,0.8824-0.8830,0.8869-0.8875,0.8846-0.8852

"Desired" tofile.csv

Code:
10/03/2013,0.9227-0.9233,0.9204-0.9210,0.9179-0.9185,0.9153-0.9159,0.9113-0.9119,0.9097-0.9103,0.9075-0.9081,0.9031-0.9037,0.9001-0.9007,0.8969-0.8975,0.8933-0.8939,0.8906-0.8912
11/03/2013,0.9153-0.9159,0.9133-0.9139,0.9111-0.9117,0.9087-0.9093,0.9063-0.9069,0.9034-0.9040,0.9008-0.9014,0.8969-0.8975,0.8933-0.8939,0.8906-0.8912,0.8887-0.8893,0.8869-0.8875
12/03/2013,0.9133-0.9139,0.9111-0.9117,0.9087-0.9093,0.9051-0.9057,0.9032-0.9038,0.8997-0.9003,0.8958-0.8964,0.8933-0.8939,0.8906-0.8912,0.8887-0.8893,0.8869-0.8875,0.8846-0.8852
13/03/2013,0.9133-0.9139,0.9111-0.9117,0.9087-0.9093,0.9051-0.9057,0.9032-0.9038,0.8993-0.8999,0.8959-0.8965,0.8920-0.8926,0.8887-0.8893,0.8869-0.8875,0.8846-0.8852,0.8824-0.8830
14/03/2013,0.9179-0.9185,0.9153-0.9159,0.9133-0.9139,0.9103-0.9109,0.9062-0.9068,0.9042-0.9048,0.9013-0.9019,0.8983-0.8989,0.8959-0.8965,0.8920-0.8926,0.8887-0.8893,0.8869-0.8875
17/03/2013,0.9153-0.9159,0.9133-0.9139,0.9103-0.9109,0.9076-0.9082,0.9049-0.9055,0.9030-0.9036,0.8993-0.8999,0.8959-0.8965,0.8920-0.8926,0.8887-0.8893,0.8869-0.8875,0.8846-0.8852

The need is just to transpose the last column from one file (fromfile.csv) to last row of the second file (the desired tofile.csv)

I have searched through the forum however I could not locate any relevant post, if this kind of query has already been posted before, please accept my sincere apologies.
# 5  
Old 03-17-2014
Assuming your input file has 13 lines:
Code:
tr -s ',' '\n' <fromfile.csv | pr -ts, -13 >tofile.csv


Last edited by ctsgnb; 03-17-2014 at 11:56 AM..
# 6  
Old 03-27-2014
Hello Akshay, Apologies for modifying the input file data. I will ensure this is not repeated in future.

Hi SriniShoo,

Thank you for providing the code, I have tested and noticed that the tofile.csv result as follow after adding an additional column in fromfile.csv.

Code:
10/03/2013,0.9227-0.9233,0.9204-0.9210,0.9179-0.9185,0.9153-0.9159,0.9113-0.9119,0.9097-0.9103,0.9075-0.9081,0.9031-0.9037,0.9001-0.9007,0.8969-0.8975,0.8933-0.8939,0.8906-0.8912
11/03/2013,0.9153-0.9159,0.9133-0.9139,0.9111-0.9117,0.9087-0.9093,0.9063-0.9069,0.9034-0.9040,0.9008-0.9014,0.8969-0.8975,0.8933-0.8939,0.8906-0.8912,0.8887-0.8893,0.8869-0.8875
12/03/2013,0.9133-0.9139,0.9111-0.9117,0.9087-0.9093,0.9051-0.9057,0.9032-0.9038,0.8997-0.9003,0.8958-0.8964,0.8933-0.8939,0.8906-0.8912,0.8887-0.8893,0.8869-0.8875,0.8846-0.8852
13/03/2013,0.9133-0.9139,0.9111-0.9117,0.9087-0.9093,0.9051-0.9057,0.9032-0.9038,0.8993-0.8999,0.8959-0.8965,0.8920-0.8926,0.8887-0.8893,0.8869-0.8875,0.8846-0.8852,0.8824-0.8830
14/03/2013,0.9179-0.9185,0.9153-0.9159,0.9133-0.9139,0.9103-0.9109,0.9062-0.9068,0.9042-0.9048,0.9013-0.9019,0.8983-0.8989,0.8959-0.8965,0.8920-0.8926,0.8887-0.8893,0.8869-0.8875
17/03/2013,0.9153-0.9159,0.9133-0.9139,0.9103-0.9109,0.9076-0.9082,0.9049-0.9055,0.9030-0.9036,0.8993-0.8999,0.8959-0.8965,0.8920-0.8926,0.8887-0.8893,0.8869-0.8875,0.8846-0.8852
,,,,,,,,,,,,
,,,,,,,,,,,,
,,,,,,,,,,,,
,,,,,,,,,,,,
,,,,,,,,,,,,
,,,,,,,,,,,,
,,,,,,,,,,,,
,,,,,,,,,,,,
,,,,,,,,,,,,
,,,,,,,,,,,,
,,,,,,,,,,,,
,,,,,,,,,,,,
,,,,,,,,,,,,
,,,,,,,,,,,,
18/03/2013
,1.1-1.2
,1.1-1.2
,1.1-1.2
,1.1-1.2
,1.1-1.2
,1.1-1.2
,1.1-1.2
,1.1-1.2
,1.1-1.2
,1.1-1.2
,1.1-1.2
,1.1-1.2

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. UNIX for Dummies Questions & Answers

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: ... (6 Replies)
Discussion started by: ganeshd
6 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