shell - Unix 中的自定义排序

标签 shell sorting unix

我想按特定顺序对以下数据进行排序。我尝试以不同的方式排序但找不到任何解决方案。请帮忙。我是 Unix 新手。:(

数据:-

method1:entry:2013.09.18.19.18.30
method1:exit:2013.09.18.19.18.30
method2:entry:2013.09.18.19.18.30
method2:exit:2013.09.18.19.18.30
method3:entry:2013.09.18.19.18.30
method4:entry:2013.09.18.19.18.30
method4:exit:2013.09.18.19.18.30
method1:entry:2013.09.18.19.18.30
method1:exit:2013.09.18.19.18.30
method3:exit:2013.09.18.19.18.30
method3:entry:2013.09.18.19.18.30
method5:entry:2013.09.18.19.18.30
method5:exit:2013.09.18.19.18.30
method3:exit:2013.09.18.19.18.30

期望的输出:-

method1:entry:2013.09.18.19.18.30
method1:exit:2013.09.18.19.18.30
method1:entry:2013.09.18.19.18.30
method1:exit:2013.09.18.19.18.30
method2:entry:2013.09.18.19.18.30
method2:exit:2013.09.18.19.18.30
method3:entry:2013.09.18.19.18.30
method3:exit:2013.09.18.19.18.30
method3:entry:2013.09.18.19.18.30
method3:exit:2013.09.18.19.18.30
method4:entry:2013.09.18.19.18.30
method4:exit:2013.09.18.19.18.30
method5:entry:2013.09.18.19.18.30
method5:exit:2013.09.18.19.18.30

排序应基于方法名称和“进入-退出”出现次数。

最佳答案

看来您只是想按方法名称排序,这是第一个冒号分隔的字段。

sort -t: -s -k1,1 file.txt

-s 标志(稳定排序)可防止 sort 修改具有相同第一个字段的行的相对顺序。

关于shell - Unix 中的自定义排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19153010/

相关文章:

linux - Bash 有条件 grep 未产生预期输出

linux - 无法建立 ssh 连接无密码运行 PVM 代码

algorithm - 线性时间排序

c - c 语言编程取决于操作系统

java - 将 jar 文件从 Windows 7 复制到 Unix 会出现 java.util.zip.ZipException

linux - Sendmail 命令将文件作为电子邮件正文和附件发送

ios - 如何在 Jenkins Groovy 的 ssh 命令中转义美元符号?

algorithm - 显示堆排序重复比较

sorting - 在 Golang 中使用 map

带重定向的 shell 内置