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. Asp.Net Output.Write()

    string name="张三" <div> <label>@Output.Write(name)</label> </div> 在 ...

  2. HDU 4549 M斐波那契数列(矩阵快速幂)

    题目链接:M斐波那契数列 题意:$F[0]=a,F[1]=b,F[n]=F[n-1]*F[n-2]$.给定$a,b,n$,求$F[n]$. 题解:暴力打表后发现$ F[n]=a^{fib(n-1)} ...

  3. [模板] Manacher(马拉车)算法

    用途 求回文子串 做法 先考虑回文子串以某字符为中心的情况,即长度为奇数 推着做,记rad[i]为以i位置为中心的最大半径(包含中点) 考虑怎么求rad[i].找之前的一个右端点最靠右的位置p,设它的 ...

  4. NOI2017蔬菜(贪心)

    小 N 是蔬菜仓库的管理员,负责设计蔬菜的销售方案. 在蔬菜仓库中,共存放有 n 种蔬菜,小 N 需要根据不同蔬菜的特性,综合考虑各 方面因素,设计合理的销售方案,以获得最多的收益. 在计算销售蔬菜的 ...

  5. LOJ#510 北校门外的回忆(找性质+倍增+线段树)

    这题一场模拟赛我们出了弱化版(n<=1e6),抄题面给的程序能拿到71分的好成绩 其实后面的29分是加了几个1e9的数据卡人 这糟老头子真是坏得很 正解我们机房看了三天 在这里感谢这篇题解的作者 ...

  6. Weblate 2.11安装配置文档

    一.系统环境: OS:CentOS 6.8 x64 Minimal HostName:Weblate IP:192.168.75.153 Python:2.7.13 pip:9.0.1 Weblate ...

  7. iview 模态框点击确定按钮不消失

    <div slot="footer"> <Button type="text" size="large" @click=& ...

  8. Django(十七)文件上传

    http://www.cnblogs.com/wupeiqi/articles/5703697.html - 文件上传        - 普通上传        - 自定义页面上传按钮        ...

  9. Django(十六)Form组件扩展

    http://www.cnblogs.com/wupeiqi/articles/6144178.html Form组件 - form表单(验证:保留上次内容) - - Ajax(验证:无需上次内容) ...

  10. 第二篇:用Android Studio编写Hello World

    将Android Studio的环境搭建好后,第一个写Hello World测试程序.Android Studio v3.2.1. 一.新建工程 点击Start a new Android Studi ...