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

  1. [leetcode shell]194. Transpose File

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

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

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

  3. 第10章:awk进阶操作

    第10章:awk进阶操作 在第4章:查找与替换简单的讲解了awk的使用,本章介绍详细讲解awk的使用.awk是一个强大的文本分析工具,简单的说awk就是把文件逐行的读入, 以空格为默认分隔符将每行切片 ...

  4. [LeetCode] Transpose File 转置文件

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

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

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

  6. Transpose File

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

  7. [LeetCode] Longest Absolute File Path 最长的绝对文件路径

    Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsub ...

  8. awk进阶整理

    BEGIN{写在前言,我英语不好,有许多地方直接使用的谷歌翻译.为了能理清awk工具使用的思路,详情还要看awk说明书(man awk) 或者http://www.gnu.org/software/g ...

  9. [LeetCode] Find Duplicate File in System 在系统中寻找重复文件

    Given a list of directory info including directory path, and all the files with contents in this dir ...

随机推荐

  1. MySQL免安装版,遇到MSVCR120.dll文件丢失错误的解决方案

    下载 VC redist packages for x64,下载完成,点击运行即可.

  2. opencv 图片剪切

    import cv2 as cv import numpy as np # 图片剪切 img = cv.imread('../images/moon.jpg', flags=1) # flags=1读 ...

  3. Nagios 配置自动发邮件功能

    安装sendmailyum install -y sendmail* mailx 修改防火墙设置,添加25端口到防火墙vi /etc/sysconfig/iptables 重启 iptables.se ...

  4. 【拓扑排序】烦人的幻灯片(slides)

    1395:烦人的幻灯片(slides) 时间限制: 1000 ms         内存限制: 65536 KB提交数: 753     通过数: 416 [题目描述] 李教授将于今天下午作一次非常重 ...

  5. linux中,使用cat、head、tail命令显示文件指定行

    小文件可以用cat(也可以用head.tail) 显示文件最后20行:cat err.log | tail -n 20 显示文件前面20行:cat err.log | head -n 20 从20行开 ...

  6. CodeForces - 589A(字符串处理)

    题目链接:http://codeforces.com/problemset/problem/589/A 题目大意:给定n个邮件地址,任何电子邮件地址都将显示为“login @ domain”,其中: ...

  7. 手把手教你用1行代码实现人脸识别 --Python Face_recognition

    环境要求: Ubuntu17.10 Python 2.7.14 环境搭建: 1. 安装 Ubuntu17.10 > 安装步骤在这里 2. 安装 Python2.7.14 (Ubuntu17.10 ...

  8. 百度地图API示例:使用vue添加删除覆盖物

    1.index.html <script type="text/javascript" src="http://api.map.baidu.com/api?v=2. ...

  9. JavaScript原型详解

    1,前言 下面是2008年Github创建以来,各种编程语言的排名情况 排名其中JavaScript自2015年之后就盘踞第一名,成为github上被使用最多的语言,早期,JS的使用还主要集中于浏览器 ...

  10. bs4抓取糗事百科

    抓取糗事百科内容及评论,不包含图片信息.user-agent填入浏览器的即可.user-agent对应的value,360极速浏览器的话,可以在地址栏输入about:version,回车,用户代理后面 ...