Need to transpose in awk rows to column like this:
input:
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:
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?
Need to transpose in awk rows to column like this:
input:
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:
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?
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:
nawk -F, '{for(i=NF;i!=1;i--) print $1, $i}' OFS=, myFile
I guess the result need be sorted.
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
Actually what I need but unsorted like in rdcwayx example. Though, I realised what I need and be more concise is this:
Input:
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:
1, A1,A3,A4
2, A1,A3
...
9, A2,A3,A4
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|"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
Hi rdcwayx ,
You are awesome in awk.
I am trying to understand your given solution/code which you have given :
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|"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
a[$i]=a[$i]==""?$1:a[$i] FS $1}
thanks