LeetCode(194.Transpose File)(awk进阶)
194. 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.
Example:
If file.txt has the following content:
name age
alice 21
ryan 30
Output the following:
name alice ryan
age 21 30
知识点:awk
awk ' ' file: ' ' 中是处理语句
BEGIN {
# 执行之前的操作,一般是赋值
}
{
# 中间是对每一行的操作
}
END {
# 处理完后的操作,一般是格式化输出
}
解法:用一个数组s,里面有NF个元素,其中NF(列号)是当前记录的总字数。对于第一个记录,直接存到相应的s[i]中,最后s[i[对应源文件一列的内容。
#!/bin/bash
awk 'BEGIN{}
{
for(i = ; i <= NF; i++){
if (NR == ){
s[i]=$i;
}
else{
s[i]=s[i]" "$i;
}
}
}END{
for(i = ; i <= NF; i++){
print s[i];
}
}' file.txt

LeetCode(194.Transpose File)(awk进阶)的更多相关文章
- [leetcode shell]194. Transpose File
Given a text file file.txt, transpose its content. You may assume that each row has the same number ...
- 刷题中熟悉Shell命令之Tenth Line和Transpose File [leetcode]
首先介绍题目中要用的4个Shell命令 sed awk head tail的常用方法.(打好地基,才能建成高楼!) sed:(转自:http://www.cnblogs.com/barrychiao/ ...
- 第10章:awk进阶操作
第10章:awk进阶操作 在第4章:查找与替换简单的讲解了awk的使用,本章介绍详细讲解awk的使用.awk是一个强大的文本分析工具,简单的说awk就是把文件逐行的读入, 以空格为默认分隔符将每行切片 ...
- [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 ...
- Transpose File
Given a text file file.txt, transpose its content. You may assume that each row has the same number ...
- [LeetCode] Longest Absolute File Path 最长的绝对文件路径
Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsub ...
- awk进阶整理
BEGIN{写在前言,我英语不好,有许多地方直接使用的谷歌翻译.为了能理清awk工具使用的思路,详情还要看awk说明书(man awk) 或者http://www.gnu.org/software/g ...
- [LeetCode] Find Duplicate File in System 在系统中寻找重复文件
Given a list of directory info including directory path, and all the files with contents in this dir ...
随机推荐
- 【XSY1537】五颜六色的幻想乡 数学 生成树计数 拉格朗日插值
题目大意 有一个\(n\)个点\(m\)条边的图,每条边有一种颜色\(c_i\in\{1,2,3\}\),求所有的包括\(i\)条颜色为\(1\)的边,\(j\)条颜色为\(2\)的边,\(k\) ...
- MT【277】华中科技大学理科实验班选拔之三次方程
(2015华中科技大学理科实验班选拔)已知三次方程$x^3+ax^2+bx+x=0$有三个实数根.(1)若三个实根为$x_1,x_2,x_3$,且$x_1\le x_2\le x_3,a,b$为常数, ...
- MT【169】拉格朗日配方
已知$x^2+y^2+z^2=1$求$3xy-3yz+2z^2$的最大值______ 答案:$3$ 提示:$3(x^2+y^2+z^2)-(3xy-3yz+2z^2)=3\left(y+\dfrac{ ...
- Android ListView item 点击事件失效问题的解决
关于ListView点击无效,item无法相应点击事件的问题,网上有很多, 大致可分为俩种情况, 一种是 item中存在 ImageButton 等可以点击的组件,这会抢先获得ListView的焦点. ...
- numpy ndarray求其最值的索引
import numpy as np a = np.array([1,2,3,4]) np.where(a== np.max(a)) >>>3 但假设其最值不止一个,如下 a = n ...
- centos7安装部署本地局域网yum源
应用场景: 当Linux系统都是最小化安装的系统,又无法做到每台都能访问外网的情况下,安装常用工具或者依赖包的最好办法可能就是建立本地yum源了. 安装环境: 一台 centos 7.4 minima ...
- Building Microservices with Spring Boot and Apache Thrift. Part 2. Swifty services
http://bsideup.blogspot.com/2015/04/spring-boot-thrift-part2.html In previous article I showed y ...
- js定时器setInterval()与setTimeout()
js定时器setInterval()与setTimeout() 1.setTimeout(Expression,DelayTime),在DelayTime过后,将执行一次Expression,setT ...
- Manjaro更新出现冲突
➜ ~ sudo pacman -Syyu :: Synchronizing package databases... core 147.5 KiB 1378K/s 00:00 [########## ...
- bash 4
1)获取字符串长度: str="jwwjww" #str=‘jwwjww’//单引号双引号都一样 echo ${#str} 结果:6 2)提取子字符串 echo ${#str:1: ...