Hi, I need to transpose columns of my files into rows and save it as individual files. sample contents of the file below.
0.9120 0.7782 0.6959 0.6904 0.6322 0.8068 0.9082
0.9290 0.7272 0.9870 0.7648 0.8053 0.8300 0.9520
0.8614 0.6734 0.7910 0.6413 0.7126 0.7364 0.8491
0.8868 0.7586 0.8949 0.6883 0.6798 0.6966 0.9124
desired output:
outfile1: outfile2: outfile3: outfile4:
0.9120 0.9290 0.8614 0.8868
0.7782 0.7272 0.6734 0.7586
0.6959 0.9870 0.7910 0.8949
0.6904 0.7648 0.6413 0.6883
0.6322 0.8053 0.7126 0.6798
0.8068 0.8300 0.7364 0.6966
0.9082 0.9520 0.8491 0.9124
bunch of thanks in advance.
joeyg
2
Have you looked at the paste command?
From your example, unclear if 'original' is one file or four files.
Hi, the original file is a single file. I need to transpose the columns of each row into columns and save it into individual files.thanks
azaria
4
Have a look at
unstableme.blogspot.com/2008/08/row-to-column-transpose-bash-scripting.html
And just google "Unix script Transpose" or maybe add ant specific shell or tool you want to use. Like ksh, or perl, etc.
Enjoy!
perl -lane 'BEGIN{$i=1}open O,">> output_$i";for(@F){print O}close O;$i++' inputfile
[root@node2 ~]# cat transpose
#!/bin/bash
transpose()
{
awk '
{
if (max_nf<NF)
max_nf=NF
max_nr=NR
for (x=1; x<=NF; ++x)
vector[x, NR]=$x
}
END {
for (x=1; x<=max_nf; ++x) {
for (y=1; y<=max_nr; ++y)
printf("%s ", vector[x, y])
printf("\n")
}
}' ${1}
}
transpose ${1}
[root@node2 ~]# cat data
0.9120 0.7782 0.6959 0.6904 0.6322 0.8068 0.9082
0.9290 0.7272 0.9870 0.7648 0.8053 0.8300 0.9520
0.8614 0.6734 0.7910 0.6413 0.7126 0.7364 0.8491
0.8868 0.7586 0.8949 0.6883 0.6798 0.6966 0.9124
[root@node2 ~]# bash transpose data
0.9120 0.9290 0.8614 0.8868
0.7782 0.7272 0.6734 0.7586
0.6959 0.9870 0.7910 0.8949
0.6904 0.7648 0.6413 0.6883
0.6322 0.8053 0.7126 0.6798
0.8068 0.8300 0.7364 0.6966
0.9082 0.9520 0.8491 0.9124
to get your file:
for i in {1..4}; do
bash transpose data | awk -v row=$i '{print $row > "outfile"row}'
done
or
[root@node2 ~]# cat run
#!/bin/bash
Transpose_matrix()
{
nr=$(wc -l < ${1})
nf=$(head -n 1 ${1} | xargs -n 1 | wc -l)
for((NF=1; NF<=${nf}; ++NF))
do
cut -d ' ' -f ${NF} ${1}
done | xargs -n ${nr}
}
Transpose_matrix ${1}
[root@node2 ~]# bash run data
0.9120 0.9290 0.8614 0.8868
0.7782 0.7272 0.6734 0.7586
0.6959 0.9870 0.7910 0.8949
0.6904 0.7648 0.6413 0.6883
0.6322 0.8053 0.7126 0.6798
0.8068 0.8300 0.7364 0.6966
0.9082 0.9520 0.8491 0.9124
to get your file:
for i in {1..4}; do
bash run data | awk -v row=$i '{print $row > "outfile"row}'
done
or
[root@node2 ~]# awk '{print $0 | "xargs -n 1 > outfile"NR }' data
Try:
awk '{f="outfile"NR;$1=$1;print>f;close(f)}' OFS="\n" infile
perl -lne 'open O,">>outfile$."; s/\s+/\n/g; print O' infile
tyler_durden
ctsgnb
9
xargs -n1 <yourfile | pr -4 -t
---------- Post updated at 04:05 PM ---------- Previous update was at 04:02 PM ----------
$ cat tst
0.9120 0.7782 0.6959 0.6904 0.6322 0.8068 0.9082
0.9290 0.7272 0.9870 0.7648 0.8053 0.8300 0.9520
0.8614 0.6734 0.7910 0.6413 0.7126 0.7364 0.8491
0.8868 0.7586 0.8949 0.6883 0.6798 0.6966 0.9124
$ xargs -n1 <tst | pr -4 -t
0.9120 0.9290 0.8614 0.8868
0.7782 0.7272 0.6734 0.7586
0.6959 0.9870 0.7910 0.8949
0.6904 0.7648 0.6413 0.6883
0.6322 0.8053 0.7126 0.6798
0.8068 0.8300 0.7364 0.6966
0.9082 0.9520 0.8491 0.9124
$ xargs -n1 <tst | pr -4ts" "
0.9120 0.9290 0.8614 0.8868
0.7782 0.7272 0.6734 0.7586
0.6959 0.9870 0.7910 0.8949
0.6904 0.7648 0.6413 0.6883
0.6322 0.8053 0.7126 0.6798
0.8068 0.8300 0.7364 0.6966
0.9082 0.9520 0.8491 0.9124