首页 > 其他 > 详细

Transpose File

时间:2015-03-28 11:22:47      阅读:182      评论:0      收藏:0      [点我收藏+]

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中的链接操作,直接将几个变量并排写就是一个新变量

Transpose File

原文:http://www.cnblogs.com/qionghua520/p/4373897.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!