热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

使用awk在同一文件中排序多个表-Orderingseveraltablesinthesamefileusingawk

Inmyworkflow,filescontainingsimpletableswithatwo-lineheader(seeendofpost)arecreated.

In my workflow, files containing simple tables with a two-line header (see end of post) are created. I want to order these tables by number using:

在我的工作流程中,创建了包含带有两行标题的简单表的文件(请参阅结尾)。我想用数字来命令这些表:

(head -n 2 && tail -n +3 | sort -n -r) > ordered.txt

That works fine, but I don't know how to split the file so that I can order every table and print it in ONE file. My approach is:

这工作正常,但我不知道如何拆分文件,以便我可以订购每个表并将其打印在一个文件中。我的方法是:

awk '/^TARGET/ {(head -n 2 && tail -n +3 | sort -n -r) >> ordered.txt}' output.txt

However, this causes an error message. I want to avoid any intermediate output files. What is missing in my awk command?

但是,这会导致错误消息。我想避免任何中间输出文件。我的awk命令缺少什么?

The input files look like that:

输入文件如下所示:

TARGET  1
Sample1 Sample2 Sample3 Pattern
3   3   3   z..........................Z........................................z.........Z...z
147 171 49  Z..........................Z........................................Z.........Z...Z
27  28  13  z..........................Z........................................z.........z...z
75  64  32  Z..........................Z........................................Z.........z...Z

TARGET  2
Sample1 Sample2 Sample3 Pattern
2   0   1   z..........................z........................................z.........Z...Z
21  21  7   z..........................Z........................................Z.........Z...Z
1   0   0   ...........................Z........................................Z.............Z
4   8   6   Z..........................Z........................................z.........Z...z
2   0   1   Z..........................Z........................................Z.........Z....
1   0   0   z..........................Z........................................Z.............Z
1   0   0   z...................................................................Z.........Z...Z

TARGET  3
Sample1 Sample2 Sample3 Pattern
1   0   0   z..........................Z........................................z.............z
1   3   0   z..........................z........................................Z.........Z...Z
1   1   0   Z..........................Z........................................Z.............z
1   0   0   Z..........................Z........................................Z.............Z
0   1   2   ...........................Z........................................Z.........Z...Z
0   0   1   z..........................z........................................z..............

My output should like that - no dropping of any line:

我的输出应该是这样的 - 不丢弃任何行:

    TARGET  1
Sample1 Sample2 Sample3 Pattern
147 171 49  Z..........................Z........................................Z.........Z...Z
75  64  32  Z..........................Z........................................Z.........z...Z
27  28  13  z..........................Z........................................z.........z...z
3   3   3   z..........................Z........................................z.........Z...z

TARGET  2
Sample1 Sample2 Sample3 Pattern
21  21  7   z..........................Z........................................Z.........Z...Z
4   8   6   Z..........................Z........................................z.........Z...z
2   0   1   z..........................z........................................z.........Z...Z
2   0   1   z..........................z........................................z.........Z...Z
1   0   0   ...........................Z........................................Z.............Z
1   0   0   ...........................Z........................................Z.............Z
1   0   0   ...........................Z........................................Z.............Z

TARGET  3
Sample1 Sample2 Sample3 Pattern
1   0   0   z..........................Z........................................z.............z
1   0   0   z..........................Z........................................z.............z
1   0   0   z..........................Z........................................z.............z
1   0   0   z..........................Z........................................z.............z
0   1   2   ...........................Z........................................Z.........Z...Z
0   0   1   z..........................z........................................z..............

2 个解决方案

#1


1  

requires GNU awk for the array traversal sorting:

需要GNU awk进行数组遍历排序:

gawk '
    BEGIN {PROCINFO["sorted_in"] = "@val_num_asc"} 
    function output_table() {
        for (key in table) print table[key]
        delete table
        i=0
    }
    /TARGET/ {print; getline; print; next} 
    /^$/ {output_table(); print; next} 
    {table[++i] = $0} 
    END {output_table()}
' file

outputs

输出

TARGET  1
Sample1 Sample2 Sample3 Pattern
3   3   3   z..........................Z........................................z.........Z...z
27  28  13  z..........................Z........................................z.........z...z
75  64  32  Z..........................Z........................................Z.........z...Z
147 171 49  Z..........................Z........................................Z.........Z...Z

TARGET  2
Sample1 Sample2 Sample3 Pattern
1   0   0   ...........................Z........................................Z.............Z
1   0   0   z...................................................................Z.........Z...Z
1   0   0   z..........................Z........................................Z.............Z
2   0   1   Z..........................Z........................................Z.........Z....
2   0   1   z..........................z........................................z.........Z...Z
4   8   6   Z..........................Z........................................z.........Z...z
21  21  7   z..........................Z........................................Z.........Z...Z

TARGET  3
Sample1 Sample2 Sample3 Pattern
0   0   1   z..........................z........................................z..............
0   1   2   ...........................Z........................................Z.........Z...Z
1   0   0   Z..........................Z........................................Z.............Z
1   0   0   z..........................Z........................................z.............z
1   1   0   Z..........................Z........................................Z.............z
1   3   0   z..........................z........................................Z.........Z...Z

#2


1  

This is a bit of a mess but assuming you dont want to lose records when you sort this should work

这有点乱,但假设你不想丢失记录,当你排序这应该工作

 awk 'function sortit(){
           x=asort(a)
           for(i=1;i<=x;i++)print b[a[i]" "d[i]++]
           delete(a);delete(b);delete(c);delete(d)
     }                             
     /^[0-9]/{a[$0]=$1;b[$1" "c[$1]++]=$0}
     /TARGET/{print;getline;print}
     !NF{sortit();print}
     END(sortit()}' file

推荐阅读
  • 本文将介绍如何编写一些有趣的VBScript脚本,这些脚本可以在朋友之间进行无害的恶作剧。通过简单的代码示例,帮助您了解VBScript的基本语法和功能。 ... [详细]
  • 本文详细解析了Python中的os和sys模块,介绍了它们的功能、常用方法及其在实际编程中的应用。 ... [详细]
  • 在维护公司项目时,发现按下手机的某个物理按键后会激活相应的服务,并在屏幕上模拟点击特定坐标点。本文详细介绍了如何使用ADB Shell Input命令来模拟各种输入事件,包括滑动、按键和点击等。 ... [详细]
  • 本教程涵盖OpenGL基础操作及直线光栅化技术,包括点的绘制、简单图形绘制、直线绘制以及DDA和中点画线算法。通过逐步实践,帮助读者掌握OpenGL的基本使用方法。 ... [详细]
  • Scala 实现 UTF-8 编码属性文件读取与克隆
    本文介绍如何使用 Scala 以 UTF-8 编码方式读取属性文件,并实现属性文件的克隆功能。通过这种方式,可以确保配置文件在多线程环境下的一致性和高效性。 ... [详细]
  • 毕业设计:基于机器学习与深度学习的垃圾邮件(短信)分类算法实现
    本文详细介绍了如何使用机器学习和深度学习技术对垃圾邮件和短信进行分类。内容涵盖从数据集介绍、预处理、特征提取到模型训练与评估的完整流程,并提供了具体的代码示例和实验结果。 ... [详细]
  • 优化局域网SSH连接延迟问题的解决方案
    本文介绍了解决局域网内SSH连接到服务器时出现长时间等待问题的方法。通过调整配置和优化网络设置,可以显著缩短SSH连接的时间。 ... [详细]
  • 深入解析Android自定义View面试题
    本文探讨了Android Launcher开发中自定义View的重要性,并通过一道经典的面试题,帮助开发者更好地理解自定义View的实现细节。文章不仅涵盖了基础知识,还提供了实际操作建议。 ... [详细]
  • 优化ListView性能
    本文深入探讨了如何通过多种技术手段优化ListView的性能,包括视图复用、ViewHolder模式、分批加载数据、图片优化及内存管理等。这些方法能够显著提升应用的响应速度和用户体验。 ... [详细]
  • Explore how Matterverse is redefining the metaverse experience, creating immersive and meaningful virtual environments that foster genuine connections and economic opportunities. ... [详细]
  • 技术分享:从动态网站提取站点密钥的解决方案
    本文探讨了如何从动态网站中提取站点密钥,特别是针对验证码(reCAPTCHA)的处理方法。通过结合Selenium和requests库,提供了详细的代码示例和优化建议。 ... [详细]
  • 本章将深入探讨移动 UI 设计的核心原则,帮助开发者构建简洁、高效且用户友好的界面。通过学习设计规则和用户体验优化技巧,您将能够创建出既美观又实用的移动应用。 ... [详细]
  • 从 .NET 转 Java 的自学之路:IO 流基础篇
    本文详细介绍了 Java 中的 IO 流,包括字节流和字符流的基本概念及其操作方式。探讨了如何处理不同类型的文件数据,并结合编码机制确保字符数据的正确读写。同时,文中还涵盖了装饰设计模式的应用,以及多种常见的 IO 操作实例。 ... [详细]
  • 尽管使用TensorFlow和PyTorch等成熟框架可以显著降低实现递归神经网络(RNN)的门槛,但对于初学者来说,理解其底层原理至关重要。本文将引导您使用NumPy从头构建一个用于自然语言处理(NLP)的RNN模型。 ... [详细]
  • 使用GDI的一些AIP函数我们可以轻易的绘制出简 ... [详细]
author-avatar
可爱竹子16
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有