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的更多相关文章

  1. LeetCode(194.Transpose File)(awk进阶)

    194. Transpose File Given a text file file.txt, transpose its content. You may assume that each row ...

  2. 刷题中熟悉Shell命令之Tenth Line和Transpose File [leetcode]

    首先介绍题目中要用的4个Shell命令 sed awk head tail的常用方法.(打好地基,才能建成高楼!) sed:(转自:http://www.cnblogs.com/barrychiao/ ...

  3. [LeetCode] Transpose File 转置文件

    Given a text file file.txt, transpose its content. You may assume that each row has the same number ...

  4. [Bash]LeetCode194. 转置文件 | Transpose File

    Given a text file file.txt, transpose its content. You may assume that each row has the same number ...

  5. [leetcode shell]194. Transpose File

    Given a text file file.txt, transpose its content. You may assume that each row has the same number ...

  6. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  7. [LeetCode] Word Frequency 单词频率

    Write a bash script to calculate the frequency of each word in a text file words.txt. For simplicity ...

  8. LeetCode Shell Problems

    195. Tenth Line -- 第十行 How would you print just the 10th line of a file? Solution: awk 'NR==10' file ...

  9. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

随机推荐

  1. smtp服务器搭建(实现本地通讯)

    1安装postfix 1)下载安装包    sudo apt-get install postfix 2)配置服务器 选择确定. 选择IneternetSite(通过SMTP发送和接收邮件),然后确定 ...

  2. HDOJ 2212 DFS

    Problem Description A DFS(digital factorial sum) number is found by summing the factorial of every d ...

  3. UVA10054 The Necklace

    UVA10054 The Necklace 链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18806 [思路] 欧拉回路 ...

  4. 模糊化GPU滤镜汇总

    GPUImageTiltShiftFilter 这是一个模糊图片上下两层的滤镜效果,可以调节模糊边界,可以调节模糊程度 总共4个参数,具体如下     //模糊度的调节,0为最清晰,后面越来越模糊   ...

  5. offsetTop offsetLeft offsetWidth offsetHeight

    document // Html 的容器对象. document.documentElement //html 对象 document.body // body 对象 $(document.docum ...

  6. SWMM[Storm Water Management Model]模型代码编译调试环境设置

    1. 下载计算引擎源代码后解压, 目前最新版本5-0-022. 其中包含源文件和工程文件,选择vc2005_con.在源代码目录下创建VC2005_CON目录拷贝VC2005-CON.VCPROJ 放 ...

  7. 月見(つきみ) 夕(ゆう) SumiHui.墨虺

    造影早迎中秋,自己通过word设计的

  8. UVa1608 UVaLive6258 Non-boring sequences

    填坑系列(p.248) 比较神 从两端枚举 最坏复杂度就成O(nlogn)了 #include<cstdio> #include<cstdlib> #include<al ...

  9. c#调用钩子

    1 概述 在c++中有钩子程序,但是在C#还没有对其进行封装,所以需要自己根据实际情况调用钩子.钩子在我的理解下是,通过初始化钩子与系统中消息映射建立某种关系,当点击鼠标或者键盘,就会通过钩子中的回调 ...

  10. winform DataGridView双击修改单元格的值 分类: DataGridView 2014-08-04 19:39 150人阅读 评论(0) 收藏

    定义全局变量 string abcmycode = "";//当前行自编号 bool tf = false;//是否双击 //双击可编辑         private void ...