Skip to content

Instantly share code, notes, and snippets.

@alexpreynolds
Created March 22, 2016 18:01
Show Gist options
  • Save alexpreynolds/177fee79142ea42bcc36 to your computer and use it in GitHub Desktop.
Save alexpreynolds/177fee79142ea42bcc36 to your computer and use it in GitHub Desktop.
Transpose matrix in awk
{
for (i = 1; i <= NF; i++) {
a[NR,i] = $i
}
}
NF > p { p = NF }
END {
for (j = 1; j <= p; j++) {
str = a[1,j]
for (i = 2; i <= NR; i++) {
str = str"\t"a[i,j];
}
print str
}
}
@alexpreynolds
Copy link
Author

$ time awk -f transpose.awk random.mtx > random.mtx.transpose-awk

real    0m17.362s
user    0m15.127s
sys     0m1.881s

$ time mawk -f transpose.awk random.mtx > random.mtx.transpose-mawk

real    0m40.647s
user    0m38.856s
sys     0m1.388s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment