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中的链接操作,直接将几个变量并排写就是一个新变量

时间: 2024-08-24 22:59:04

Transpose File的相关文章

leetcode 上的 bash 程序

*/--> pre.src {background-color: Black; color: White;} leetcode 上的 bash 程序 Table of Contents Tenth Line Transpose File Valid Phone Numbers Word Frequency 注: 以下程序大部份从 leetcode 的讨论上看来的... Tenth Line How would you print just the 10th line of a file? For

[LeetCode] Word Frequency 单词频率

Write a bash script to calculate the frequency of each word in a text file words.txt. For simplicity sake, you may assume: words.txt contains only lowercase characters and space ' ' characters. Each word must consist of lowercase characters only. Wor

leetcode 的shell部分4道题整理

对shell的某些细节还不是十分熟悉,借鉴了好多别人的东西 1. Word Frequency 此题很简单,只要能排序就可以 cat words.txt |tr -s " " "\n" sort | unique -c | sort -r | awk '{print $2" "$1}' 2. Valid Phone Numbers cat file.txt | awk '/^\d{3}-\d{3}-\d{4}$/| /^\([0-9]{3}\)

LeetCode Problems List 题目汇总

No. Title Level Rate 1 Two Sum Medium 17.70% 2 Add Two Numbers Medium 21.10% 3 Longest Substring Without Repeating Characters Medium 20.60% 4 Median of Two Sorted Arrays Hard 17.40% 5 Longest Palindromic Substring Medium 20.70% 6 ZigZag Conversion Ea

【leetcode】bash脚本练习

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } span.Apple-tab-span { white-space: pre } [192]Word Frequency Write a bash script to calculate the frequency of each word in a text file words.txt. For simplicity sake, you may assume: w

JavaSE8基础 File lastModified 获取文件夹的修改日期

os :windows7 x64    jdk:jdk-8u131-windows-x64    ide:Eclipse Oxygen Release (4.7.0) information: 通过这张截图可以看到 测试文件夹 的修改日期. code: package jizuiku0; import java.io.File; import java.text.SimpleDateFormat; import java.util.Date; /* * @version V17.09 */ pu

JavaSE8基础 File createNewFile 在一个不存在的文件夹中创建文件时 会抛IO异常(绝对路径)

os :windows7 x64    jdk:jdk-8u131-windows-x64    ide:Eclipse Oxygen Release (4.7.0) information: 在编写代码时,javase8文件夹的情况截图. code: package jizuiku0; import java.io.File; import java.io.IOException; /* * @version V17.09 */ public class FileDemo_1 { public

JavaSE8基础 File getAbsolutePath 获取当前默认的工作文件夹路径

os :windows7 x64    jdk:jdk-8u131-windows-x64    ide:Eclipse Oxygen Release (4.7.0) code: package jizuiku0; import java.io.File; /* * @version V17.09 */ public class GetDemo { public static void main(String[] args) { System.out.println("当前的默认工作路径是:&q

JavaSE8基础 File FilenameFilter 筛选出指定后缀名的文件

os :windows7 x64    jdk:jdk-8u131-windows-x64    ide:Eclipse Oxygen Release (4.7.0) code: package jizuiku0; import java.io.File; import java.io.FilenameFilter; /* * @version V17.09 */ public class Filter { public static void main(String[] args) { //