Grep 多个文件,按输出排序

Grep 多个文件,按输出排序

我正在使用 grep 搜索多个日志文件。在日志中,每行的第一部分均采用 hh:mm:ss 格式的时间。这次应该是排序标准。

如果我使用grep -r string1 | grep --color string2 | sort排序将应用于文件名(这是有用的信息),添加参数-n会使事情变得更糟,因为将使用包含的行号。

日志的形式如下:

354058:c1.com/client-01.log:13:30:04 INFO:       Item: created widget in ARRIVALS
360746:c1.com/client-01.log:13:39:46 INFO:          change transfer to contact
379144:c3.com/client-01.log:13:30:02 INFO:       Item: created widget in ARR
392162:c3.com/client-01.log:13:51:59 INFO:       Item: created widget in ARR
367422:c5.com/client-01.log:13:51:56 INFO:       searching for Sign 
367446:c5.com/client-01.log:13:51:59 INFO:       Item: created widget in ARRIVALS
367629:c5.com/client-01.log:13:52:12 INFO:          change from initial to contact
371979:c9.com/client-01.log:14:00:19 INFO:          change workflow to tgl

编辑:我期望的类型是:

379144:c3.com/client-01.log:13:30:02 INFO:       Item: created widget in ARR
354058:c1.com/client-01.log:13:30:04 INFO:       Item: created widget in ARRIVALS
360746:c1.com/client-01.log:13:39:46 INFO:          change transfer to contact
367422:c5.com/client-01.log:13:51:56 INFO:       searching for Sign 
392162:c3.com/client-01.log:13:51:59 INFO:       Item: created widget in ARR
367446:c5.com/client-01.log:13:51:59 INFO:       Item: created widget in ARRIVALS
367629:c5.com/client-01.log:13:52:12 INFO:          change from initial to contact
371979:c9.com/client-01.log:14:00:19 INFO:          change workflow to tgl

根据建议的更改和添加,| sort -t'/' -k2V大多数结果似乎都是合理的,但我突然得到了这样的结果,而大多数其他输出则完全没问题。

353619:d1.com/core-01.log:14:09:45 INFO:          Server: updating
33:c5.com/client-01.log:13:30:02 INFO:       Item: created widget in ACTIVE

答案1

给定文件

354058:c1.com/client-01.log:13:30:04 INFO:       Item: created widget in ARRIVALS
360746:c1.com/client-01.log:13:39:46 INFO:          change transfer to contact
379144:c3.com/client-01.log:13:30:02 INFO:       Item: created widget in ARR
392162:c3.com/client-01.log:13:51:59 INFO:       Item: created widget in ARR
353619:d1.com/core-01.log:14:09:45 INFO:          Server: updating
367422:c5.com/client-01.log:13:51:56 INFO:       searching for Sign
367446:c5.com/client-01.log:13:51:59 INFO:       Item: created widget in ARRIVALS
367629:c5.com/client-01.log:13:52:12 INFO:          change from initial to contact
371979:c9.com/client-01.log:14:00:19 INFO:          change workflow to tgl

命令

sort -t: -k3 file

:将使用第三个分隔字段(及其后的任何内容)作为排序键来执行字典排序。结果将是

379144:c3.com/client-01.log:13:30:02 INFO:       Item: created widget in ARR
354058:c1.com/client-01.log:13:30:04 INFO:       Item: created widget in ARRIVALS
360746:c1.com/client-01.log:13:39:46 INFO:          change transfer to contact
367422:c5.com/client-01.log:13:51:56 INFO:       searching for Sign
392162:c3.com/client-01.log:13:51:59 INFO:       Item: created widget in ARR
367446:c5.com/client-01.log:13:51:59 INFO:       Item: created widget in ARRIVALS
367629:c5.com/client-01.log:13:52:12 INFO:          change from initial to contact
371979:c9.com/client-01.log:14:00:19 INFO:          change workflow to tgl
353619:d1.com/core-01.log:14:09:45 INFO:          Server: updating

为此,假设字段 3、4 和 5 中的数字始终用零填充。

相关内容