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. [HNOI2008]玩具装箱TOY(斜率优化)

    题目链接 题意:有编号为\(1\cdots N\)的N件玩具,第 i 件玩具经过压缩后变成一维长度为 \(C_i\)​ .要求在一个容器中的玩具编号是连续的,同时如果将第 i 件玩具到第 j 个玩具放 ...

  2. Hdoj 2501.Tiling_easy version 题解

    Problem Description 有一个大小是 2 x n 的网格,现在需要用2种规格的骨牌铺满,骨牌规格分别是 2 x 1 和 2 x 2,请计算一共有多少种铺设的方法. Input 输入的第 ...

  3. 「SDOI2014」重建 解题报告

    「SDOI2014」重建 题意 给一个图\(G\),两点\((u,v)\)有边的概率是\(p_{u,v}\),求有\(n-1\)条边通行且组成了一颗树的概率是多少. 抄了几个矩阵树定理有趣的感性说法 ...

  4. Redis主从复制与高可用方案

    redis简单介绍 Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据库.Redis与其他key – value缓存产品有以下三个特点: 支持数据的持久化,可以将内存中 ...

  5. angular2路由与express路由冲突的问题

    angular2的路由定义了一个/a,如果走angular的路由没问题,如果直接访问/a就会出现cannot GET /a的错误,原因就是express的路由问题. 所以路由走angular2,那ex ...

  6. poj1442 Black Box

    The Black Case 好啊! 首先,读题很艰难... 读完题,发现是求第k小的数,那么我们用splay水过对顶堆水过即可. #include <cstdio> #include & ...

  7. k短路(A*)

    http://poj.org/problem?id=2449 #include <cstdio> #include <cstdlib> #include <cstring ...

  8. Good Bye 2018

    Good Bye 2018 2018年最后一场CF,OVER! 弱弱的我只能做出3道A,B,D~~~~ 最后几分钟,感觉找到了C题的规律,结束的那一刻,提交了一发 "Wrong answer ...

  9. Python_反射

    利用字符串的形式去对象中寻找成员 导入单个模块: commons为公共模块,inp为输入 func=getattr(commons,inp) 利用反射最大的好处是不用单个单个导入,而通过getattr ...

  10. (进制转换 栈)P1143 进制转换 洛谷

    题目描述 请你编一程序实现两种不同进制之间的数据转换. 输入输出格式 输入格式: 共三行,第一行是一个正整数,表示需要转换的数的进制n(2≤n≤16),第二行是一个n进制数,若n>10n> ...