简体   繁体   English

Linux grep 并对日志文件进行排序

[英]Linux grep and sort log files

I looked almost everywhere ( there ,there , there , there and there ) with no luck.我几乎无处不在( 那里那里那里那里那里),但没有运气。

What I have here is a bunch of log files in a directory, where I need to look for a specific ID (myID) and sort the output by date.我这里有一个目录中的一堆日志文件,我需要在其中查找特定的 ID (myID) 并按日期对 output 进行排序。 Here is an example:这是一个例子:

in file1.log:在 file1.log 中:

2015-09-26 15:39:50,788 - DEBUG - blabla : {'id' : myID}

in file2.log:在 file2.log 中:

2015-09-26 15:39:51,788 - ERROR - foo : {'id' : myID}

in file3.log:在 file3.log 中:

2015-09-26 15:39:48,788 - ERROR - bar : {'id' : myID}

Exepected output:预计 output:

2015-09-26 15:39:48,788 - ERROR - bar : {'id' : myID}
2015-09-26 15:39:50,788 - DEBUG - blabla : {'id' : myID}
2015-09-26 15:39:51,788 - ERROR - foo : {'id' : myID}

What I am doing now (and it works pretty well), is:我现在正在做的(而且效果很好)是:

grep -hri --color=always "myID" | sort -n

The only problem is that with the -h option of grep, the file names are hidden.唯一的问题是使用 grep 的 -h 选项,文件名被隐藏了。 I'd like to keep the file names AND keep the sorting.我想保留文件名并保持排序。 I tried:我试过了:

grep -ri --color=always "myID" | sort -n -t ":" -k1,1 -k2,2

But it doesn't work.但它不起作用。 Basically, the grep command outputs the name of the file followed by ":", I'd like to sort the results from this character.基本上,grep 命令输出文件名后跟“:”,我想从这个字符对结果进行排序。

Thanks a lot非常感谢

Try this: 尝试这个:

grep --color=always "myID" file*.log | sort -t : -k2,2 -k3,3n -k4,4n

Output: 输出:

file3.log:2015-09-26 15:39:48,788 - ERROR - bar : {'id' : myID}
file1.log:2015-09-26 15:39:50,788 - DEBUG - blabla : {'id' : myID}
file2.log:2015-09-26 15:39:51,788 - ERROR - foo : {'id' : myID}

Another solution, a little bit longer but I think it should work: 另一个解决方案,稍微长一点,但我认为它应该工作:

 grep -l "myID" file* > /tmp/file_names && grep -hri "myID" file* | sort -n > /tmp/grep_result && paste /tmp/file_names /tmp/grep_result | column -s $'\t' -t

What it does basically is, first store files names by: 它的作用基本上是,首先通过以下方式存储文件名:

grep -l "myID" file* > /tmp/file_names

Store grep sorted results: 存储grep排序结果:

grep -hri "myID" file* | sort -n > /tmp/grep_result 

Paste the results column-wise (using a tab separator): 逐列粘贴结果(使用制表符分隔符):

paste /tmp/file_names /tmp/grep_result | column -s $'\t' -t

The column ordering for sort is 1-based, so k1 will be your filename part. 排序的列排序是从1开始的,因此k1将是您的文件名部分。 That means that in your attempt, you are sorting by filename, then by date and hour of your log line. 这意味着在您的尝试中,您按文件名排序,然后按日志行的日期和小时排序。 Also, the -n means that you are using numeric ordering, which won't be playing nicely with yyyy-mm-dd hh:mm:ss format (it will read yyyy-mm-dd hh as only the first number, ie the year). 此外, -n表示您正在使用数字排序,它不能与yyyy-mm-dd hh:mm:ss格式很好地匹配(它将只读取yyyy-mm-dd hh作为第一个数字,即年)。

You can use: 您可以使用:

sort -t ":" -k2

Note that I specified column 2 as the start, and left the end blank. 请注意,我将第2列指定为开头,并将结尾留空。 The end defaults to the end-of-line. 结束默认为行尾。

If you want to sort specific columns, you need to explicitly set the start and end, for example: -k2,2 . 如果要对特定列进行排序,则需要显式设置开始结束,例如: -k2,2 You can use this to sort out-of-sequence columns, for example -k4,4 -k2,2 will sort by column 4 and use column 2 for tie-breaking. 您可以使用它来对无序列进行排序,例如-k4,4 -k2,2将按列4排序,并使用第2列进行打破平局。

You could also use -k2,4 , which would stop sorting at the colon just before your log details (ie it would use 2015-09-26 15:39:48,788 - ERROR - bar ) 您也可以使用-k2,4 ,它会在您的日志详细信息之前停止在冒号处排序(即它将使用2015-09-26 15:39:48,788 - ERROR - bar

Finally, perhaps you want to have your log files in a consistent order if the time is the same: 最后,如果时间相同,也许您希望将日志文件保持一致的顺序:

sort -t ":" -k2,4 -k1,1

Try rust-based tool Super Speedy Syslog Searcher试用基于 Rust 的工具Super Speedy Syslog Searcher

(assuming you have rust installed ) (假设您安装了rust

cargo install super_speedy_syslog_searcher

then然后

s4 file1.log file2.log file3.log | grep "myID"

The only problem is that with the -h option of grep, the file names are hidden.唯一的问题是使用 grep 的 -h 选项,文件名被隐藏了。 I'd like to keep the file names AND keep the sorting.我想保留文件名并保持排序。

You could try你可以试试

$ s4 --color=never -nw file1.log file2.log file3.log | grep "myID"
file1.log:2015-09-26 15:39:48,788 - ERROR - bar : {'id' : myID}
file2.log:2015-09-26 15:39:50,788 - DEBUG - blabla : {'id' : myID}
file3.log:2015-09-26 15:39:51,788 - ERROR - foo : {'id' : myID}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM