Transpose File
Given a text file file.txt
, transpose its content.
You may assume that each row has the same number of columns and each field is separated by the ' '
character.
For example, if file.txt
has the following content:
name age alice 21 ryan 30
Output the following:
name alice ryan age 21 30
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" "a[i,j]; } print str } }' file.txt
http://stackoverflow.com/questions/1729824/transpose-a-file-in-bash
领悟:
1、awk中二位数组的定义
2、循环控制的上线(NR,NF)
3、awk中的链接操作,直接将几个变量并排写就是一个新变量