awk转置文本

输入

name age
alice 21
ryan 30
输出

name alice ryan
age 21 30
代码

#!/bin/awk -f
{
        for(i=1;i<=NF;i++){
                if(NR == 1){
                        a[i]=$i;
                }else{
                        a[i]=a[i]" "$i;
                }
        }
}
END {
        for(i=1;a[i]!="";i++){
                print a[i];
        }
}

参考:

https://leetcode.com/discuss/29462/ac-solution-using-awk-and-statement-just-like-c

http://coolshell.cn/articles/9070.html

你可能感兴趣的:(Linux)