transpose column and rows using gawk
awk, gawk, row, transpose
Solution
I don't see why it will not be - unless you don't have enough memory. Try the below and see if you run into problems.
Input:
$ cat inf.txt
a b c d
1 2 3 4
. , + -
A B C D
Awk program:
$ cat mkt.sh
awk '
{
for(c = 1; c <= NF; c++) {
a[c, NR] = $c
}
if(max_nf < NF) {
max_nf = NF
}
}
END {
for(r = 1; r <= NR; r++) {
for(c = 1; c <= max_nf; c++) {
printf("%s ", a[r, c])
}
print ""
}
}
' inf.txt
Run:
$ ./mkt.sh
a 1 . A
b 2 , B
c 3 + C
d 4 - D
Credits:
- http://www.chemie.fu-berlin.de/chemnet/use/info/gawk/gawk_12.html#SEC121
Hope this helps.
Problem
I am trying to transpose a really long file and I am concerned that it will not be transposed entirely. My data looks something like this: ``` Thisisalongstring12345678 1 AB abc 937 4.320194 Thisisalongstring12345678 1 AB efg 549 0.767828 Thisisalongstring12345678 1 AB hi 346 -4.903441 Thisisalongstring12345678 1 AB jk 193 7.317946 ``` I want my data to look like this: ``` Thisisalongstring12345678 Thisisalongstring12345678 Thisisalongstring12345678 Thisisalongstring12345678 1 1 1 1 AB AB AB AB abc efg hi jk 937 549 346 193 4.320194 0.767828 -4.903441 7.317946 ``` Would the length of the first string prove to be an issue? My file is much longer than this approx 2000 lines long. Also is it possible to change the name of the first string to Thisis234, and then transpose?