Replace rows to column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace rows to column
# 1  
Old 08-31-2014
Replace rows to column

I have a csv file and i want to convert its rows into columns

sample file like this

Code:
Row1,1,2,3,......,n
row2,4,5,6,.......,n
.
.
.
.
rown,7,8,9,........,n

i want it like this


Code:
row1,row2,....,rown
1,4,.............,7
2,5,.............,8
3,6,.............,9


is it possible to do so.

Last edited by Don Cragun; 08-31-2014 at 05:39 PM.. Reason: Change QUOTE tags to CODE tags.
# 2  
Old 08-31-2014
Almost certainly, and probably done before in these fora. Any attempts from your side? And, please use code tags, not quote tags.
# 3  
Old 09-01-2014
Dear RudiC,

plz let me the logic how to do it, because the no of rows and columns are variable (may change every time).
# 4  
Old 09-01-2014
Hi Sagar,

Here is an awk approach. Lets say we have an example input file as follows.

Code:
cat test15
Row1    Row2    Row3    Row4    Row5
435 BL_lmapm03 rrr RDF1+TDEV 0cef 45 mask1
435 BL_lmapm03 rrr TDEV 080a 50 mask2
435 BL_lmapm02 fff RDF1+TDEV 0ceg 45 mask4
435 BL_lmapm02 fff TDEV 080b 60 mask6
435 BL_lmapm06 hhh TDEV 080f 60 mask9

Then code will be as follows.
Code:
awk '{if(NR==1) {print $0} else {for(i=1;i<=NF;i++){a_i[i]=a_i[i]?a_i[i] OFS $i:$i}}} END{for(i=1;i<=NF;i++){print a_i[i]}}' test15

Output will be as follows.

Code:
Row1    Row2    Row3    Row4    Row5
435 435 435 435 435
BL_lmapm03 BL_lmapm03 BL_lmapm02 BL_lmapm02 BL_lmapm06
rrr rrr fff fff hhh
RDF1+TDEV TDEV RDF1+TDEV TDEV TDEV
0cef 080a 0ceg 080b 080f
45 50 45 60 60
mask1 mask2 mask4 mask6 mask9

Hope this will be helpful for you.


Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 5  
Old 09-01-2014
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Replace space in column with letter for several rows

I have a pbd file, which has the following format: TITLE Protein X MODEL 1 ATOM 1 N PRO 24 45.220 71.410 43.810 1.00 0.00 ATOM 2 H1 PRO 24 45.800 71.310 42.000 1.00 0.00 TER ENDMDL Column 22 is the chain... (5 Replies)
Discussion started by: Egy
5 Replies

2. Shell Programming and Scripting

Replace a character of specified column(s) of all rows in a file

Hi - I have a file "file1" of below format. Its a comma seperated file. Note that each string is enclosed in double quotes. "abc","-0.15","10,000.00","IJK" "xyz","1,000.01","1,000,000.50","OPR" I want the result as: "abc","-0.15","10000.00","IJK" "xyz","1,000.01","1000000.50","OPR" I... (8 Replies)
Discussion started by: njny
8 Replies

3. Shell Programming and Scripting

Converting Single Column into Multiple rows, but with strings to specific tab column

Dear fellows, I need your help. I'm trying to write a script to convert a single column into multiple rows. But it need to recognize the beginning of the string and set it to its specific Column number. Each Line (loop) begins with digit (RANGE). At this moment it's kind of working, but it... (6 Replies)
Discussion started by: AK47
6 Replies

4. UNIX for Dummies Questions & Answers

[Solved] Deleting all rows where the first column equals the second column

Hi, I have a tab delimited text file where the first two columns equal numbers. I want to delete all rows where the value in the first column equals the second column. How do I go about doing that? Thanks! Input: 1 1 ABC DEF 2 2 IJK LMN 1 2 ZYX OPW Output: 1 2 ZYX OPW (2 Replies)
Discussion started by: evelibertine
2 Replies

5. UNIX for Dummies Questions & Answers

merging rows into new file based on rows and first column

I have 2 files, file01= 7 columns, row unknown (but few) file02= 7 columns, row unknown (but many) now I want to create an output with the first field that is shared in both of them and then subtract the results from the rest of the fields and print there e.g. file 01 James|0|50|25|10|50|30... (1 Reply)
Discussion started by: A-V
1 Replies

6. Shell Programming and Scripting

awk command to print only selected rows in a particular column specified by column name

Dear All, I have a data file input.csv like below. (Only five column shown here for example.) Data1,StepNo,Data2,Data3,Data4 2,1,3,4,5 3,1,5,6,7 3,2,4,5,6 5,3,5,5,6 From this I want the below output Data1,StepNo,Data2,Data3,Data4 2,1,3,4,5 3,1,5,6,7 where the second column... (4 Replies)
Discussion started by: ks_reddy
4 Replies

7. UNIX for Dummies Questions & Answers

Converting column to rows for every 3 lines in the column

Hi gurus! Please help me with this one. I have an file with the following contents: a b c d e f g h i j I would like to make to transform it to look like this as my output file: a,b,c d,e,f (4 Replies)
Discussion started by: kokoro
4 Replies

8. Shell Programming and Scripting

Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2

Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2 file 1 sample SNDK 80004C101 AT XLNX 983919101 BB NETL 64118B100 BS AMD 007903107 CC KLAC 482480100 DC TER 880770102 KATS ATHR 04743P108 KATS... (7 Replies)
Discussion started by: rydz00
7 Replies

9. UNIX for Dummies Questions & Answers

How to sum rows in e.g. column 1 by a category in e.g. column 2

Hi, I've shown an example of what I would like to achieve below. In the example file, I would like to sum the values in column 2 for each distinct category in column 3 (presumably making an array?) and print the sum as well as the category name and length (note:length always corresponds with... (8 Replies)
Discussion started by: auburn
8 Replies

10. Shell Programming and Scripting

column to rows based on another column...

Guys, i have a file in below format where the barcode's are uniq per site but could be repeated for different site. so i want to convert the site column to rows based on the barcode's as below output. your help is appreciated!!! input: SITE BARCODE QTY SP CP 10001 6281103890017 10 50 48... (5 Replies)
Discussion started by: malcomex999
5 Replies
Login or Register to Ask a Question