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 说明:题目中含有$符号则为付费题目. 如 ...
随机推荐
- unity3d 制造自己的水体water effect(一)
first,I wish you a happy new year, and study in spring festival’s eve means you are hardworking,haha ...
- 全面理解Java异常的运行机制
1. 引子 try…catch…finally恐怕是大家再熟悉不过的语句了,而且感觉用起来也是很简单,逻辑上似乎也是很容易理解.不过,我亲自体验的“教训”告诉我,这个东西可不是想象中的那么简单.听话. ...
- 《A First Course in Probability》-chaper8-极限定理-弱大数定理
基于之前强大数定理的得证,这里我们再结合切比雪夫不等式,能够得到弱大数定理. 弱大数定理: 表面上,强大数定理和弱大数定理好像是质同的,但是他们之间真正的区别到底是什么呢?
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(34)-文章发布系统①-简要分析
原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(34)-文章发布系统①-简要分析 系列目录 最新比较闲,为了学习下Android的开发构建ASP.NET ...
- 移植opencv库到zedboard(制作运行库镜像) 分类: OpenCV ZedBoard ubuntu shell Eye_Detection 2014-11-08 18:48 172人阅读 评论(0) 收藏
主要参考rainysky的博客 http://ledage.eefocus.com/sj229335457/blog/13-06/295352_ad954.html opencv的话只需要将lib这个 ...
- JSF学习五Ajax
验证username(不能有下划线)和password(不能小于六位) 1.UserBean.java package ajax; import java.io.Serializable; impor ...
- android 05 桢布局:FrameLayout 网格布据 GridLayout
xml文件: <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android: ...
- 亿图图示专家v7.7破解版
软件简介 亿图图示专家是一款综合矢量绘制软件,新颖小巧,功能强大,可以很方便的绘制各种专业的流程图.组织结构图.网络拓扑图.思维导图.商业图表.科学设计图等.轻轻松松绘制各种专业流程图,网络图,思维导 ...
- [转] linux下查看文件编码及修改编码
如果无法识别文件编码,可能是文件中已有乱码,此时需要去掉乱码 查看文件编码 在Linux中查看文件编码可以通过以下几种方式: 1.在Vim中可以直接查看文件编码 :set fileencoding 即 ...
- Java基础知识强化04:判断101~200之间有多少素数
1. 判断101~200之间有多少素数? package himi.hebao; /** * (1).编写函数isPrime()用来判断输入数据是否为素数 (2).遍历判断101~200之间的数据是否 ...