码迷,mamicode.com
首页 > 其他好文 > 详细

Transpose File

时间:2015-03-28 11:23:49      阅读:126      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!