Transpose File
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的更多相关文章
- LeetCode(194.Transpose File)(awk进阶)
194. Transpose File Given a text file file.txt, transpose its content. You may assume that each row ...
- 刷题中熟悉Shell命令之Tenth Line和Transpose File [leetcode]
首先介绍题目中要用的4个Shell命令 sed awk head tail的常用方法.(打好地基,才能建成高楼!) sed:(转自:http://www.cnblogs.com/barrychiao/ ...
- [LeetCode] Transpose File 转置文件
Given a text file file.txt, transpose its content. You may assume that each row has the same number ...
- [Bash]LeetCode194. 转置文件 | Transpose File
Given a text file file.txt, transpose its content. You may assume that each row has the same number ...
- [leetcode shell]194. Transpose File
Given a text file file.txt, transpose its content. You may assume that each row has the same number ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- [LeetCode] Word Frequency 单词频率
Write a bash script to calculate the frequency of each word in a text file words.txt. For simplicity ...
- LeetCode Shell Problems
195. Tenth Line -- 第十行 How would you print just the 10th line of a file? Solution: awk 'NR==10' file ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
随机推荐
- HTMLTestRunner生成空白resault.html
发现一奇葩问题,用idle或pyscripter执行脚本,生成的是空白html,通过cmd,进入脚本目录执行python xx.py,却能生成测试报告. HTMLTestRunner 例子 #codi ...
- Hibernate(三)Hibernate 配置文件
在上次的博文Hibernate(二)Hibernate实例我们已经通过一个实例的演示对Hibernate 的基本使用有了一个简单的认识,这里我们在此简单回顾一下Hibernate框架的使用步骤. Hi ...
- java多线程编程(1) 线程的基本知识
在前面研究过多线程与进程的区别. 这里在稍微总结一下: 进程:程序动态的一次执行过程. 线程:可以只是程序员的一部分的执行过程 每个进程有多个线程组成,在java程序中,至少两个线程一个是垃圾回收线程 ...
- leetcode https://oj.leetcode.com/problems/jump-game-ii/
1.超时的,效率太低 public class Solution { public int jump(int[] A) { int len=A.length; int d[]=new int[len] ...
- Hibernate的Session会话中get()和load()方法的区别
1.get和load都可以从数据库中获取数据 .get拿到的是真的对象,load拿到的是代理对象 2.get和load从数据库中获取数据,如果获取不到,get返回null,load会出现ObjectN ...
- [MIREX] MIREX评测介绍
MIREX作为国际最权威音频检索评测大赛,竟然在百度上找不到任何介绍,只有几个与什么搜狗.腾讯获得什么成绩相关的检索内容,相比而言,TRECVID的内容收到重视多了...由于研究生阶段主要研究音频领域 ...
- AutoLayout--masonry使用
[label1 mas_makeConstraints:^(MASConstraintMaker *make) { //使左边间距为 make.left.equ ...
- FZU 2104 (13.11.28)
Problem 2104 Floor problem Accept: 376 Submit: 433 Time Limit: 1000 mSec Memory Limit : 32768 ...
- 【设计模式 - 11】之享元模式(FlyWeight)
1 模式简介 当系统中存在大量对象时,非常容易造成内存溢出.为了解决这个问题,我们把这些对象中共有的部分抽象出来,如果有相同的业务请求,则直接返回在内存中已有的对象,避免重新创建,这就是享元 ...
- Linux查看系统资源占用
Linux查看系统资源占用 在系统维护的过程中,随时可能有需要查看 CPU和内存的使用率,并根据相应信息分析系统状况的需求.本文介绍一下几种常见的Linux系统资源查看命令. 1.总体内存占用的查看 ...