[Home]
HomePage RecentChanges Frequently Asked Questions

Transpose

To transpose a N rows x M columns file into a M rows x N column file, one approach is to read the whole file in memory as a bidimensional array, and finally print it transposing rows and columns (the example assumes comma separated fields; adapt to your real data):

BEGIN{FS=","}
{for(i=1;i<=NF;i++)a[NR,i]=$i}
END{
 for(i=1;i<=NF;i++){
 for(j=1;j<=NR;j++){
 line=line sep a[j,i]
 sep=FS
 }
 print line
 line=sep=""
 }
}

NR is our N, and NF is our M here. In the END block, for M times, the output is built line-by-line and after N values are concatenated, the line is printed and set back to empty, ready for building the next line.


Edit this page View other revisions Administration
Last edited 2011年03月20日 06:53 UTC by ppp089210038204.dsl.hol.gr (diff)

Powered by sdf Creative Commons License <pierre.gaston+awk@gmail.com>

Awk Wiki by #awk is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.

AltStyle によって変換されたページ (->オリジナル) /