首页 > 其他分享 >LeetCode //Bash - 194. Transpose File

LeetCode //Bash - 194. Transpose File

时间:2024-07-01 14:57:57浏览次数:15  
标签:transposed File 194 Transpose file each line txt

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

From: LeetCode
Link: 194. Transpose File


Solution:

Ideas:
  1. Reading lines and columns: awk processes the file line by line. For each field in the line, it appends the field to the corresponding index in the transposed array.

  2. Constructing the transposed lines: As we read each line, we concatenate the fields to build the transposed rows. The NR variable tracks the line number.

  3. Printing the transposed result: After processing all lines (END block), we print each element of the transposed array.

Code:
# Read from the file file.txt and print its transposed content to stdout.

awk '
{
    for (i = 1; i <= NF; i++) {
        if (NR == 1) {
            transposed[i] = $i
        } else {
            transposed[i] = transposed[i] " " $i
        }
    }
}
END {
    for (i = 1; i <= NF; i++) {
        print transposed[i]
    }
}' file.txt

标签:transposed,File,194,Transpose,file,each,line,txt
From: https://blog.csdn.net/navicheung/article/details/140090780

相关文章

  • C# 压缩和解压缩文件 (System.IO.Compression.ZipFile)
    参考文档:https://learn.microsoft.com/zh-cn/dotnet/api/system.io.compression.zipfile?view=net-8.0&redirectedfrom=MSDNhttps://blog.csdn.net/lvmingzhou/article/details/134161441 操作zip存档及其文件的方法分布在三个类中:ZipFile、ZipArchive和ZipArchiveEntry......
  • Java方法递归:File文件搜索
        在Java中,方法递归是一种特殊的情况,其中方法直接或间接地调用自身。为了使用方法递归,方法需要有基本情况,即不再调用自身的条件,以防止进入无限循环。    我们来做一个搜索文件并打开的案例。以打开QQ为例,因为我的电脑只有C盘,我搜索文件的地方,就写C盘。publ......
  • 农业新质生产力数据(2012-2022年)原始+dofile+测算数据集
    数据简介:农业新质生产力是指在现代农业发展中,通过融合尖端科技、信息技术与创新管理模式,实现农业生产效率飞跃、产品质量显著提升及生产可持续性增强的一种革新性生产能力,农业新质生产力代表了从依赖传统资源转向依靠科技创新与高效资源配置的农业现代化路径,是推动农业绿色转型......
  • java的输入流FileInput Stream类
    一、定义使用InputStream类的FileInputStream子类实现文本文件内容的读取。二、常用构造方法三、使用FileInputStream类按多字节读取数据1.示例 2、分析四、常见错误  今天的总结就到这里啦,拜拜!  ......
  • 【日记】原来真的有人不适合谈恋爱(1194 字)
    正文21日正是周五,夏至。全年当中,白天时长最长的一天。而恰好那天也是银行扣息的日子。所以很忙,我差点没能走掉。所幸最终还是有惊无险。到斯的家里,是晚上9点钟。比我想得要早。这个周周四,他过生日。但是那天因为上班,所以移到了周末。不是法定节假日,很普通的一个......
  • A Completed Python Scripter and File Handle with Matplotlib
    importosimportrequestsimportrequestsfrombs4importBeautifulSoupfromtypingimportDicttotal_div:Dict[str,BeautifulSoup]=dict()defthe_big_div(text:str):soup=BeautifulSoup(text,'lxml')count=soup.find_all('div......
  • java的输出流File OutputStream
    一、字节输出流FileOutputStream 1、定义使用OutputStream类的FileOutputStream子类向文本文件写入的数据。2.常用构造方法3.创建文件输出流对象的常用方式 二、输出流FileOutputStream类的应用示例1.示例  2、实现步骤  今天的总结就到此结束啦,拜拜! ......
  • 大数据运维学习笔记之filebeat+kafka+MM1跨机房实时日志传输案例——筑梦之路
    日志数据量:日均30亿  ......
  • 如何使用xlsx和file-saver插件实现导入导出
    首先,安装xlsx和file-saver插件在组件中引入import*asXLSXfrom'xlsx';importFileSaverfrom'file-saver';<div>  <!--导入表格-->  <labelfor="import-excel">导入表格:</label>  <inputid="import-excel&qu......
  • 解决Linux中出现Too many open files
    Too many open files  问题出现有两种情况:一种是在搜索的时候出现,多半是由于索引创建完毕之后被移动过,如果创建索引的时候不出现该错误,搜索的时候也一般是不会出现的。如果出现了,有两种处理办法,一种是修改合并因子和最小合并因子,并且使用IndexWriter.Optimize()  优化索引,......