awk transpose rows to column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk transpose rows to column
# 1  
Old 10-27-2011
awk transpose rows to column

Need to transpose in awk rows to column like this:

input:
HTML Code:
A1,6,5,4 3,2,1, 
A2,8,7,9,10,11,12,13,14
A3,1,2,3,5,7,8,9
A4,9,4,8,1,5,3,
output:
HTML Code:
A1,1
A1,2
A1,4
...
A2,7
A2,8
...
A3,1
A3,2
...
A4,1
A4,3
A4,4
A4,5
...
Can anybody help?
# 2  
Old 10-27-2011
this is not really a matrix transposition - this is a hybrid of some kind. And your desired output is not consistent - in particular the A3 and the A4 sequences...
A better/consistent example, please!

Maybe something like this:
Code:
nawk -F, '{for(i=NF;i!=1;i--) print $1, $i}' OFS=, myFile

This User Gave Thanks to vgersh99 For This Post:
# 3  
Old 10-27-2011
I guess the result need be sorted.
Code:
awk -F , '{for (i=2;i<=NF;i++) if ($i>=0) print $1 FS $i|"sort -t, -k1,1 -k2,2n"}' infile

A1,1
A1,2
A1,4 3
A1,5
A1,6
A2,7
A2,8
A2,9
A2,10
A2,11
A2,12
A2,13
A2,14
A3,1
A3,2
A3,3
A3,5
A3,7
A3,8
A3,9
A4,1
A4,3
A4,4
A4,5
A4,8
A4,9


Last edited by rdcwayx; 10-27-2011 at 09:24 PM..
This User Gave Thanks to rdcwayx For This Post:
# 4  
Old 10-28-2011
Quote:
Originally Posted by vgersh99
this is not really a matrix transposition - this is a hybrid of some kind. And your desired output is not consistent - in particular the A3 and the A4 sequences...
A better/consistent example, please!
Actually what I need but unsorted like in rdcwayx example. Though, I realised what I need and be more concise is this:

Input:
HTML Code:
A1,6,5,4 3,2,1
A2,8,7,9,10,11,12,13,14
A3,1,2,3,5,7,8,9
A4,9,4,8,1,5,3,
output:
HTML Code:
1, A1,A3,A4
2, A1,A3
...
9, A2,A3,A4
# 5  
Old 10-28-2011
Code:
awk -F, '{for (i=2;i<=NF;i++) a[$i]=a[$i]==""?$1:a[$i] FS $1}
    END{for (i in a) if (i>0)print i FS a[i]|"sort -n"}' infile 

1,A1,A3,A4
2,A1,A3
3,A3,A4
4 3,A1
4,A4
5,A1,A3,A4
6,A1
7,A2,A3
8,A2,A3,A4
9,A2,A3,A4
10,A2
11,A2
12,A2
13,A2
14,A2

This User Gave Thanks to rdcwayx For This Post:
# 6  
Old 11-23-2011
Hi rdcwayx ,

You are awesome in awk.

I am trying to understand your given solution/code which you have given :
Code:
awk -F, '{for (i=2;i<=NF;i++) a[$i]=a[$i]==""?$1:a[$i] FS $1}
    END{for (i in a) if (i>0)print i FS a[i]|"sort -n"}' infile

I am able to understand till "for loop"
after that I am not getting how it works , can you please explain the rest of part how it is executing.

specially, below one
Code:
a[$i]=a[$i]==""?$1:a[$i] FS $1}

thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to transpose pieces of data in a column to multiple rows?

Hello Everyone, I am very new to the world of regular expressions. I am trying to use grep/sed for the following: Input file is something like this and there are multiple such files: abc 1 2 3 4 5 ***END*** abc 6 7 8 9 ***END*** abc 10 (2 Replies)
Discussion started by: shellnewuser
2 Replies

2. Shell Programming and Scripting

Linux - Transpose rows into column

hello, I have a server that collect some performance statistics of 4 servers in the following input file : $ cat inputfile Time,A,Server1,KPI1,data1 Time,A,Server1,KPI2,data2 Time,A,Server1,KPI3,data3 Time,A,Server1,KPI4,data4 Time,A,Server1,KPI5,data5 Time,A,Server2,KPI1,data6... (9 Replies)
Discussion started by: capitain25
9 Replies

3. Programming

To transpose rows to column in hadoop

Hi, i am having an HDFS file which is comma seperated, i need to transpose from rows to column only the header columns text.csv cnt,name,place 1,hi,nz 2,hello,aus I need cnt, name, place while using below command in hadoop getting the error hadoop fs -fmt -1 text.csv (0 Replies)
Discussion started by: rohit_shinez
0 Replies

4. Shell Programming and Scripting

Peel syntax for transpose rows into column

Dear all, Plz let me know syntax for transposing rows into column in perl, I am having 30 csv files which are merged into a single xls sheet. but i want to transpose each row into column in excel sheet in each tab (1 CSV = 1tab in xls sheet) example is as below ... (0 Replies)
Discussion started by: sagar_1986
0 Replies

5. Shell Programming and Scripting

Transpose data as rows using awk

Hi I have below requirement, need help One file contains the meta data information and other file would have the data, match the column from file1 and with file2 and extract corresponding column value and display in another file File1: CUSTTYPECD COSTCENTER FNAME LNAME SERVICELVL ... (1 Reply)
Discussion started by: ravlapo
1 Replies

6. Shell Programming and Scripting

awk to transpose every 7 rows into columns

input: a1 a2 a3 a4 a5 a6 a7 b1 b2 b3 .. b7 .. z1 .. z7 (12 Replies)
Discussion started by: ux4me
12 Replies

7. Shell Programming and Scripting

Transpose multiple rows (with a mix of space and enter) to a single column

How to change the uploaded weekly file data to the following format? New Well_Id,Old Well_Id,District,Thana,Date,Data,R.L,WellType,Lati.,Longi. BAG001,PT006,BARGUNA,AMTALI,1/2/1978,1.81,2.29,Piezometer,220825,901430 BAG001,PT006,BARGUNA,AMTALI,1/9/1978,1.87,2.29,Piezometer,220825,901430... (3 Replies)
Discussion started by: sara.nowreen
3 Replies

8. Shell Programming and Scripting

Transpose Column of Data to Rows

I can no longer find my commands, but I use to be able to transpose data with common fields from a single column to rows using a command line. My data is separated as follows: NAME=BOB ADDRESS=COLORADO PET=CAT NAME=SUSAN ADDRESS=TEXAS PET=BIRD NAME=TOM ADDRESS=UTAH PET=DOG I would... (7 Replies)
Discussion started by: docdave78
7 Replies

9. Shell Programming and Scripting

awk to transpose preceding row to 1st column of next rows

Gurus: How can I transpose the output below to a format in which I can plot a graph to show VSZ memory usage by PIDs? stdout: Tue Jan 22 07:29:19 CUT 2013 42336296 1841272 java wilyadm 21889232 438616 jlaunch sidadm 42532994 414336 jlaunch sidadm Tue Jan 22 07:49:20 CUT 2013... (1 Reply)
Discussion started by: ux4me
1 Replies

10. Shell Programming and Scripting

Transpose Datefield from rows to column + Print time diff

Hi Experts, Can you please help me in transposing Datefield from rows to column and calculate the time difference for each of the Jobids: Input File: 08/23/2012 12:36:09,JOB_5340 08/23/2012 12:36:14,JOB_5340 08/23/2012 12:36:22,JOB_5350 08/23/2012 12:36:26,JOB_5350 Required Output:... (6 Replies)
Discussion started by: asnandhakumar
6 Replies
Login or Register to Ask a Question