linux - 在 linux shell 中将我的输入文本转换为输出的最佳方法是什么

标签 linux bash shell unix ksh

我正在尝试在 Linux 中格式化以下文本文件。你能建议什么是实现我的输出的最佳方法吗?

输入文字

Header| Header Identifier
[2017-02-03 14:23:44,066] - Message 1
[2017-02-03 14:23:45,066] - Message 1
[2017-02-03 14:23:46,066] - Message 1
[2017-02-03 14:23:47,066] - Message 1
Trailer | Trailer Identifer
Header| Header Identifier
[2017-02-03 14:23:44,066] - Message 2
[2017-02-03 14:23:45,066] - Message 2
[2017-02-03 14:23:46,066] - Message 2
[2017-02-03 14:23:47,066] - Message 2
Trailer | Trailer Identifer

我正在尝试生成的输出

Header| Header Identifier
[2017-02-03 14:23:44,066] - Message 1
Trailer | Trailer Identifer
Header| Header Identifier
[2017-02-03 14:23:44,066] - Message 2
Trailer | Trailer Identifer

提前致谢!

更新:我不想在这里删除重复项,我想保留时间戳最少的记录并删除其他记录。

最佳答案

有一个 bash 命令:uniq。输入命令:

uniq -f3 file

您很幸运,该命令仍然适用于您的用例,无需使用 awk 进行编程。我引用了手册页的一部分:

uniq 过滤来自 INPUT(或标准输入)的 adjacent 匹配行,写入 OUTPUT(或标准输出)。

选项 -f3 避免比较前 3 个字段。

关于linux - 在 linux shell 中将我的输入文本转换为输出的最佳方法是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42021384/

相关文章:

c - Scandir自带过滤功能

linux - 在 Ubuntu Bash 中,如何将变量与标准输出值进行比较?

linux - 获取在 Linux 系统上使用的正确以太网接口(interface)

bash - 管道符号 |在 AWK 字段分隔符中

java - 使用 bash globing 设置 Java 命令行属性

bash - 如何检查是否以 root 身份在 bash 脚本中运行

linux - 编写运行其他用户拥有的另一个 shell 脚本的 shell 脚本

python - .format 在 shell 脚本中等效

linux - 如何在 docker 实例中关闭 sendfile

Linux shell : how to remove a string starting with a pattern from an input file