Hi.
This demonstration code:
#!/usr/bin/env bash
# @(#) s1 Demonstrate separate, transpose, sort, transpose, combine file matrix.
# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
em() { pe "$*" >&2 ; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C cut transpose.pl sort paste pass-fail
FILE=${1-data1}
N=${FILE//[A-Za-z]/}
E=expected-output$N
# Utility functions: print-as-echo, print-line-with-visual-space.
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
pl " Input data file $FILE:"
head $FILE
pl " Expected output:"
head $E
pl " Prepare input, split and save first 4 columns, remainder:"
cut -f1-4 $FILE > first-four
cut -f5- $FILE |
tee remainder
pl " Results, transpose, sort:"
transpose.pl remainder |
tee t2 |
sort -k1.2,1n |
tee t3
pl " Results, re-transpose, paste:"
transpose.pl t3 > sorted-remainder
paste first-four sorted-remainder | tee f1
pl " Verify results if possible:"
C=$HOME/bin/pass-fail
[ -f $C ] && $C f1 "$E" || ( pe; pe " Results cannot be verified." ) >&2
exit
produces:
$ ./s1
Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution : Debian 8.9 (jessie)
bash GNU bash 4.3.30
cut (GNU coreutils) 8.23
transpose.pl - ( local: RepRev 1.1, ~/bin/transpose.pl, 2017-01-29 )
sort (GNU coreutils) 8.23
paste (GNU coreutils) 8.23
pass-fail (local) 1.10
-----
Input data file data1:
a b c d V10 V2 V8 V4 V7
xx z y 1000 1 2 0 2 0
tr v m 1001 0 0 1 2 2
rg s n 1003 1 1 2 0 0
-----
Expected output:
a b c d V2 V4 V7 V8 V10
xx z y 1000 2 2 0 0 1
tr v m 1001 0 2 2 1 0
rg s n 1003 1 0 0 2 1
-----
Prepare input, split and save first 4 columns, remainder:
V10 V2 V8 V4 V7
1 2 0 2 0
0 0 1 2 2
1 1 2 0 0
-----
Results, transpose, sort:
V2 2 0 1
V4 2 2 0
V7 0 2 0
V8 0 1 2
V10 1 0 1
-----
Results, re-transpose, paste:
a b c d V2 V4 V7 V8 V10
xx z y 1000 2 2 0 0 1
tr v m 1001 0 2 2 1 0
rg s n 1003 1 0 0 2 1
-----
Verify results if possible:
-----
Comparison of 4 created lines with 4 lines of desired results:
Succeeded -- files (computed) f1 and (standard) expected-output1 have same content.
For some other codes that can transpose, see:
Transpose
1) rs, reshape a data arrays
2) transpose.pl
http://www1.cuni.cz/~obo/textutils/
3) transpose, sourceforge c
https://sourceforge.net/projects/transpose/
4) pspp
'FLIP' transposes rows and columns in the active dataset.
5) datamash
transpose transpose rows, columns of the input file
*) awk, perl, python, c, R, sc, and so on:
http://stackoverflow.com/questions/1729824/an-efficient-way-to-transpose-a-file-in-bash
http://stackoverflow.com/questions/25331830/how-do-i-efficiently-transpose-a-matrix-in-r
https://www.unix.com/unix-for-beginners-questions-and-answers/270683-transpose-large-data-unix.html et al
I also tried the solution with item 3 above, and it worked.
I tried an alternate file with fewer columns, and it seemed to work.
Some details on transpose.pl and transpose:
transpose.pl Swap rows and columns in the given tab-delimited table (MR). (what)
Path : ~/bin/transpose.pl
Version : - ( local: RepRev 1.1, ~/bin/transpose.pl, 2017-01-29 )
Length : 28 lines
Type : Perl script, ASCII text executable
Shebang : #!/usr/bin/perl
Home : http://www1.cuni.cz/~obo/textutils/ (doc)
transpose Reshapes delimited text data (help)
Path : ~/executable/transpose
Version : - ( local: ~/executable/transpose, 2017-01-29 )
Type : ELF64-bitLSBexecutable,x86-64,version1(SYSV ...)
Home : https://sourceforge.net/projects/transpose/ (doc)
Best wishes ... cheers, drl