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. Linux学习笔记30——套接字

    一 什么是套接字 套接字是一种通信机制,凭借这种机制,客户/服务器系统的开发既可以在本地单机上进行,也可以跨网络进行. 二 套接字属性 套接字的特性由3个属性确定,它们是:域,类型和协议   1 套接 ...

  2. stringstream 与空格 (大家讨论一下代码结果的原因)

    #include <iostream> // std::cout, std::endl #include <iomanip> // std::setw #include < ...

  3. leetcode 最大矩形和

    1.枚举法(超时) public class Solution { public int largestRectangleArea(int[] height) { int max=-1; for(in ...

  4. 怎么把GPUImageFIlter处理过的图像保存成UIImage

    总共有两种方法能够把GPUImage处理过的图片转化成UIImage 方法一:     UIImage *inputImage = [UIImage imageNamed:@"Lambeau ...

  5. xapian倒排索引的归并流程

    Xapian的检索流程和大部分搜索系统都一样,就先从倒排表抽取候选文档,然后结合其他信息进行排序,取top文档作为搜索结果,具体流程如下: 图1 xapian搜索流程 具体流程 在terms中找到do ...

  6. linux trap

  7. first blog编程之美-----计算1的个数

    根据以下总结写出以下程序,总结来源于网上 感想:得硬着头皮找规律 #include   int count1(int n) {         int i=1;         int count=0 ...

  8. 几个命令行命令的总结(node, babel-cli, babel-node)

    node: 输入node, 进入repl环境之后,可以直接运行javascsript表达式,模拟node输出 sh-it-nb0023:static xialei$ node > console ...

  9. 【Jsoup爬取网页内容】

    思路:根据给定URL分析其源码,得到所需的网页内容的位置,制定规则采集或下载之 采集的图片和文字示例: tags: tag:brazil tag:dog tag:pet tag:pointyfaced ...

  10. c# 发送邮件、附件 分类: C# 2014-12-17 16:41 201人阅读 评论(0) 收藏

    WinForm窗体代码如下: <span style="font-size:14px;">using System; using System.Collections. ...