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

What is the best way to convert my input text to output in linux shell

我正在尝试格式化 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 过滤器 adjacent 匹配来自 INPUT(或标准输入)的行,写入 OUTPUT(或标准输出)。

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

@Abhishek Narayan:尝试:

awk --re-interval '/[0-9]{4}-[0-9]{2}-[0-9]{2}/ && !Q{print;Q=1;next} !/[0-9]{4}-[0-9]{2}-[0-9]{2}/{Q="";print}'  Input_file

考虑到您的 Input_file 已对日期进行排序。我正在检查日期的正则表达式,然后检查名为 Q 的值是否为空,打印该行并将 Q 的值设置为 1。因此,当行与日期模式不匹配时,它会将变量 Q 的值设置为空并打印这条线。