Saturday, October 29, 2022

[SOLVED] Linux grep and sort log files

Issue

I looked almost everywhere (there, href="https://stackoverflow.com/questions/8680561/bash-sort-by-regexp">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. Here is an example :

in file1.log :

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

in file2.log:

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

in file3.log:

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

Exepected 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. 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.

Thanks a lot


Solution

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}


Answered By - Cyrus
Answer Checked By - Robin (WPSolving Admin)