mskcc
July 19, 2005, 9:57pm
1
Hello,
Is there a way to transpose a file in shell scripting? For instance, from
a1 a2 a3 a4 a5 a6 a7 ....
b1 b2 b3 b4 b5 b6 b7 ....
c1 c2 c3 c4 c5 c6 c7 ....
d1 d2 d3 d4 d5 d6 d7 ...
...
...
...
to
a1 b1 c1 d1 ....
a2 b2 c2 d2 ....
a3 b3 c3 d3 ....
a4 b3 c3 d4 ....
...
...
...
Thanks in advance.
reborg
July 19, 2005, 10:07pm
2
Yes there is, the question has been answered here several times. Try using the search functionality with keywords "row", "column" and "transpose".
Ygor
July 19, 2005, 10:33pm
3
Try...
awk '{
for (f = 1; f <= NF; f++)
a[NR, f] = $f
}
NF > nf { nf = NF }
END {
for (f = 1; f <= nf; f++)
for (r = 1; r <= NR; r++)
printf a[r, f] (r==NR ? RS : FS)
}' file1 > file2
mskcc
July 29, 2005, 4:18pm
4
Thanks, but the code didn't work. Any idea? why
no, not from your description - works fine here.
mskcc
July 29, 2005, 4:49pm
6
Wired, My file is a multi-column based tab delimited file with uneven rows. It needs to be transposed to row based txt file. This is code,
bash-2.03$ more file_transpose
###file transpose, colume to row
###from Unix Linux Community - Technical support for all Unix and Linux users , 07/29/2005
awk '{
for (f = 1; f <= NF; f++)
a[NR, f] = $f
}
NF > nf { nf = NF }
END {
for (f = 1; f <= nf; f++)
for (r = 1; r <= NR; r++)
printf a[r, f] (r==NR ? RS : FS)
}' atcc_fidler_pvs_all_markers.txt > atcc_fidler_pvs_all_markers_out.txt
The error message is,
sorgerlab-x% bash
bash-2.03$ ./file_transpose
awk: syntax error near line 3
awk: illegal statement near line 3
awk: syntax error near line 9
awk: illegal statement near line 9
Thanks for the help
mskcc
July 29, 2005, 4:50pm
7
by the way, how to search the archives here.
mskcc:
Wired, My file is a multi-column based tab delimited file with uneven rows. It needs to be transposed to row based txt file. This is code,
bash-2.03$ more file_transpose
###file transpose, colume to row
###from Unix Linux Community - Technical support for all Unix and Linux users , 07/29/2005
awk '{
for (f = 1; f <= NF; f++)
a[NR, f] = $f
}
NF > nf { nf = NF }
END {
for (f = 1; f <= nf; f++)
for (r = 1; r <= NR; r++)
printf a[r, f] (r==NR ? RS : FS)
}' atcc_fidler_pvs_all_markers.txt > atcc_fidler_pvs_all_markers_out.txt
The error message is,
sorgerlab-x% bash
bash-2.03$ ./file_transpose
awk: syntax error near line 3
awk: illegal statement near line 3
awk: syntax error near line 9
awk: illegal statement near line 9
Thanks for the help
If on Solaris, try using 'nawk' instead of plain 'awk'
by clicking the 'Search' and filling in the drop-down menu.
Using Ruby:
[[1,2,3], [4,5,6]].transpose
yields
[[1, 4], [2, 5], [3, 6]]
mskcc
August 3, 2005, 2:56pm
11
Thank you guys. It worked by change awk to nawk, but the output is not tab delimited format. Can anyone fix it for me? thx.
nawk -v OFS='\t' ..........
mskcc
August 3, 2005, 4:31pm
13
Thanks! It worked well. But if I need to transpose a file like this to a colume based file, how can I do it? Please help. Thanks in advance.
can you elaborate on the question, pls?
mskcc
August 4, 2005, 9:17am
15
Thanks. The problem is somehow just a opposite of the original one. Basically, we need to transpose a row based file to a column based file, please see below. Since the Excel has a limitation of 256 columns, it is almost useless to those who want to transpose a big file.
file format(tab delimited):
chip1 a1 b1 c1 d1 ......
chip2 a2 b2 c2 d2 .............
chip3 a3 b3 c3 d3 .........
....
after transpose(tab delimited),
chip1 chip2 chip3 chip4 ...
a1 b1 c1 d1 ...
a2 b2 c2 d2 ...
a3 b3 c3 d3 ...
....
mskcc
August 4, 2005, 9:20am
16
sorry, it's wrong.
after transpose,
chip1 chip2 chip3 ...
a1 a2 a3 ...
b1 b2 b3 ...
c1 c2 c3 ...
....
mskcc:
sorry, it's wrong.
after transpose,
chip1 chip2 chip3 ...
a1 a2 a3 ...
b1 b2 b3 ...
c1 c2 c3 ...
....
how's that different from the original?
the output I get is:
chip1 chip2 chip3
a1 a2 a3
b1 b2 b3
c1 c2 c3
d1 d2 d3
mskcc
August 4, 2005, 9:36am
18
original was column to row, but now is row to column.
have you tried running the script?
isn't the above output what you wanted?
mskcc
August 4, 2005, 10:12am
20
yes, original input was,
chip1 chip2 chip3 ...
a1 a2 a3 ...
b1 b2 b3 ...
c1 c2 c3 ...
...
output was,
chip1 a1 b1 c1 ...........
chip2 a2 b2 c2 ......
chip3 a3 b3 c3 ..............
...